synctest.java

来自「由浅入深的介绍JAVAse的基本编程思想」· Java 代码 · 共 46 行

JAVA
46
字号
public class SyncTest{
    public static void main(String args[]){
		SyncStack stack = new SyncStack();
		Runnable p=new Producer(stack);
		Runnable c = new Consumer(stack);
		Thread t1 = new Thread(p);
		Thread t2 = new Thread(c);
		t1.start();
		t2.start();
    }
}


class  Producer implements Runnable{
	SyncStack stack;	
	public Producer(SyncStack s){
		stack = s;
	}
	public void run(){
		for(int i=0; i<20; i++){
			char c =(char)(Math.random()*26+'A');
			stack.push(c);
			try{							        
				Thread.sleep((int)(Math.random()*300));
			}catch(InterruptedException e){
			}
		}
	}
}

class Consumer implements Runnable{
	SyncStack stack;	
	public Consumer(SyncStack s){
		stack = s;
	}
	public void run(){
		for(int i=0;i<20;i++){
			char c = stack.pop();
			try{							           
				Thread.sleep((int)(Math.random()*500));
			}catch(InterruptedException e){
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?