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