test.java

来自「thread」· Java 代码 · 共 53 行

JAVA
53
字号
package com.test2;

import java.util.Stack;
import java.util.Vector;

public class Test {
	
	private int index;

	private int[] is = new int[6];

	public synchronized int pop() {
		while(index == 0){
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		index--;
		int temp = is[index];
		
		notifyAll();
		
		return temp;
	}

	public synchronized void push(int i) {
		while(index == 6){
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		is[index] = i;
		++index;
		
		notifyAll();
	}

	public static void main(String[] args) {
		Test t = new Test();
		T1 t1 = new T1(t);
		T2 t2 = new T2(t);
		t1.start();
		t2.start();
	}

}

⌨️ 快捷键说明

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