testthreadmethod.java

来自「《Java程序设计与应用》-张仕斌-源程序 《Java程序设计与应用》-张仕斌」· Java 代码 · 共 40 行

JAVA
40
字号
/*
 * TestThreadMethod.java
 *
 * Created on 2006年10月19日, 下午12:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package example9_6;

/**
 *
 * @author Administrator
 */
public class TestThreadMethod extends Thread{
    
    /** Creates a new instance of TestThreadMethod */
    public TestThreadMethod() {
    }
 public static int shareVar = 0;
	public TestThreadMethod(String name){super(name);}
	public synchronized void run(){
		if(shareVar==0){
			for(int i=0; i<10; i++){
				shareVar++;
				if(shareVar==5){
					try{this.wait();} //语句1
					catch(InterruptedException e){}
				}
			}
		}
		if(shareVar!=0){
			System.out.print(Thread.currentThread().getName());
			System.out.println(" shareVar = " + shareVar);
			this.notify();//语句2
		}
	}   
}

⌨️ 快捷键说明

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