现在的位置: 首页 > 综合 > 正文

Java Thread 11

2013年08月20日 ⁄ 综合 ⁄ 共 576字 ⁄ 字号 评论关闭
package com.jue.test;

public class MainClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread thread = new MyThread();
		thread.start();
		try{Thread.sleep(1000);}catch(Exception e){}
		synchronized(thread){
			System.out.println("Main-thread wait");
			try {
				thread.wait();
			} catch (InterruptedException e) {}
			System.out.println("Main-thread result = " + thread.total);
		}
	}

}

class MyThread extends Thread {
	int total = -1;

	@Override
	public void run() {
		synchronized(this){
			for(int i = 0; i < 101; i++){
				total += i;
			}
			System.out.println("MyThread-notify");
			notify();
		}
	}
}

result:

MyThread-notify
Main-thread wait

抱歉!评论已关闭.