📄 threadcom1.java
字号:
class Q
{
private String name="cat";
private String sex="a";
boolean bFull=false;
synchronized void put(String name,String sex)
{
try{if(bFull)wait();}catch(Exception e){
System.out.println(e.getMessage());}
this.name=name;
try{Thread.sleep(10);}catch(Exception e){
System.out.println(e.getMessage());}
this.sex=sex;
bFull=true;
notify();
}
synchronized void get(){
try{if(!bFull)wait();}catch(Exception e){
System.out.println(e.getMessage());}
System.out.println(name+"----->"+sex);
bFull=false;
notify();
}
}
class Produce implements Runnable
{
Q q=null;
public Produce(Q q){this.q=q;}
public void run()
{
int i=0;
while (true)
{
if(i==0)q.put("dog","d");else q.put("cat","a");
i=(i+1)%2;
}
}
}
class Consume implements Runnable
{
Q q=null;
public Consume(Q q){this.q=q;}
public void run()
{
while (true)
{
q.get();
}
}
}
public class ThreadCom1
{
public static void main(String [] args)
{
Q q=new Q();
new Thread(new Produce(q)).start();
new Thread(new Consume(q)).start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -