5.17 JAVA

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5.17 JAVA相关的知识,希望对你有一定的参考价值。

class Computer{
int num=0;
boolean flag=false;
public synchronized void set(){
if(flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
num++;
System.out.println("生产了第"+num+"台电脑。");
flag=true;
super.notify();
}
public synchronized void get(){
if(!flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("取走了第"+num+"台电脑。");
flag=false;
super.notify();
}
}
class Customer implements Runnable{
private Computer com;

public void setCompu(Computer compu){
this.com=compu;
}
public Computer getCompu(){
return this.com;
}
public Customer(Computer com){
this.com=com;
}
public void run(){
for(int i=0;i<10;i++){
com.set();
com.get();
}
}
}
public class Test9_2 {
public static void main(String[] args) {
Computer a=new Computer();
Customer cus=new Customer(a);
new Thread(cus).start();
}
}

技术分享

以上是关于5.17 JAVA的主要内容,如果未能解决你的问题,请参考以下文章

5.17 JAVA

5.17日记

5.17

5.17上午

5.17 英语单词小记

py 5.17