?? common.java
字號(hào):
package com.test;
public class common
{
private char ch;
private boolean available = false;
synchronized char get()
{
while(available==false)
{
try
{
wait();
}catch(InterruptedException e){}
available = false;
notify();
return ch;
}
return ch;
}
synchronized void put(char newch)
{
while(available==true)
{
try
{
wait();
}catch(InterruptedException e){}
ch = newch;
available = true;
notify();
}
}
//生產(chǎn)者線程
static class producer extends Thread
{
private common comm;
public producer(common thiscomm)
{
comm = thiscomm;
}
public void run()
{
for(char c='a';c<='e';c++)
{
System.out.println("生產(chǎn)的數(shù)據(jù)是:"+c);
comm.put(c);
}
}
};
//消費(fèi)者線程
static class consumer extends Thread
{
private common comm;
public consumer(common thiscomm)
{
comm = thiscomm;
}
public void run()
{
char c;
for(int i=0;i<5;i++)
{
c = comm.get();
System.out.println("消費(fèi)者得到的數(shù)據(jù)是:"+c);
}
}
};
public static void main(String arg[])
{
common comm = new common();
producer p = new producer(comm);
consumer s = new consumer(comm);
p.start();
s.start();
}
};
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -