您的位置:首页 > 大数据 > 人工智能

Toy Program——线程同步。涉及wait(),notifyall(),runnable,toString

2016-05-20 14:17 330 查看
public class TestProducerConsumer {

public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer pd = new Producer(ss);
Consumer cs = new Consumer(ss);
new Thread(pd).start();
new Thread(pd).start();
new Thread(pd).start();
new Thread(cs).start();
new Thread(cs).start();
}

}

class SteamedBuns{
int id = 0;
SteamedBuns(int id){
this.id = id;
}
public String toString(){
return "SteamedBuns: " + id;
}
}

class Producer implements Runnable{
SyncStack sbsBuffer = null;
Producer(SyncStack sbsBuffer){
this.sbsBuffer = sbsBuffer;
}

public void run(){
for(int i = 0; i < 20; i++){
SteamedBuns sbs = new SteamedBuns(i);
sbsBuffer.putInto(sbs);
System.out.println("+++Produced+++ " + sbs);
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
}

class Consumer implements Runnable{
SyncStack sbsBuffer = null;
Consumer(SyncStack sbsBuffer){
this.sbsBuffer = sbsBuffer;
}

public void run(){
for(int i = 0; i < 20; i++){
SteamedBuns sbs = sbsBuffer.takeOut();
System.out.println("--Consumed-- " + sbs);
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
}
class SyncStack{
int index = 0;
SteamedBuns[] arrSbs = new SteamedBuns[20];

public synchronized void putInto(SteamedBuns sbs){//线程锁定与唤醒
while(index == arrSbs.length){
try{
this.wait();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
this.notifyAll();
arrSbs[index] = sbs;
index++;
}

public synchronized SteamedBuns takeOut(){//线程锁定与唤醒
while(index == 0){
try{
this.wait();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
this.notifyAll();
index--;
return arrSbs[index];
}
}

当线程需要阻塞式的等待时,则调用wait()。而wait()的同时,锁止权不由线程控制。

sleep()全程hold锁止权。

wait()搭配notify()和notifyAll()使用。

能用Interface的Runnable就不用class的Thread
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息