mythread.java

来自「Threads为JAVA线程序例子集合.能方便了解JAVA线程的运作」· Java 代码 · 共 34 行

JAVA
34
字号
public class MyThread extends Thread { 
int count= 1, number; 
public MyThread(int num) { 
number = num; 
System.out.println("创建线程 " + number); 
} 
public void run() {
 count();

}

public   synchronized void count(){
	try{
		int i=0;
			while(count<11) { 
			System.out.println("线程 " + number + ":计数 " + count); 
			i+=2;
			count+=i;
				if(i==2){
	 				this.notify();
				}else{
					this.wait();
				}
			}
		}
	catch(InterruptedException e){
		e.printStackTrace();
		}
 
	}
public static void main(String args[]) { 
for(int i = 0; i < 5; i++) new MyThread(i+1).start();
} 
}

⌨️ 快捷键说明

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