当线程A获得了obj锁后,发现条件condition不满足,无法继续下一处理,于是线程A就wait()。
在另一线程B中,如果B更改了某些条件,使得线程A的condition条件满足了,就可以唤醒线程A:

解决方案 »

  1.   

    public class TestAlpha {

        public static void main(String[] args) throws InterruptedException {
        
         new A().start();
        
         new B().start();    
        
        
    }
    }class Resource {

    private static Resource instance = new Resource();

    static Resource getInstance(){
    return instance;
    }

    private boolean condition = false; public boolean isCondition() {
    return condition;
    } public void setCondition(boolean condition) {
    this.condition = condition;
    }
    }class A extends Thread{

    public void run(){
    synchronized (Resource.getInstance()) {
    if (false == Resource.getInstance().isCondition()) {
    try {
    System.out.println(" wait for ....");
    Resource.getInstance().wait();
    } catch (Exception dx) {
    dx.printStackTrace();
    }
    }
    }

    System.out.println(" do ......");
    }
    }
    class B extends Thread{
    public void run(){

    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
        
    synchronized (Resource.getInstance()) {
    Resource.getInstance().setCondition(true);
    Resource.getInstance().notify();
    }
    }
    }
      

  2.   

    WorldClock.java
    import java.awt.*;
    import java.util.*;public class WorldClock{
    Frame f=new Frame();
    Label l1=new Label();
    Label l2=new Label();
    Label l3=new Label();
    Label l4=new Label();
    Label l5=new Label();
    Label l6=new Label();

    public WorldClock(){
    f.setLayout(new GridLayout(2,3));

    l1.setFont(new Font("Arial",Font.BOLD,30));
    l2.setFont(new Font("Arial",Font.BOLD,30));
    l3.setFont(new Font("Arial",Font.BOLD,30));
    l4.setFont(new Font("Arial",Font.BOLD,20));
    l5.setFont(new Font("Arial",Font.BOLD,20));
    l6.setFont(new Font("Arial",Font.BOLD,20));
    l4.setText("北京时间        ");
    l4.setForeground(Color.red);
    l5.setText("纽约时间        ");
    l5.setForeground(Color.red);
    l6.setText("巴黎时间        ");
    l6.setForeground(Color.red);
    f.add(l1);
    f.add(l2);
    f.add(l3);
    f.add(l4);
    f.add(l5);
    f.add(l6);

    TimeZone t1=TimeZone.getDefault();
    TimeZone t2=TimeZone.getTimeZone("US/Alaska");
    TimeZone t3=TimeZone.getTimeZone("Europe/Paris");

            Runner r1=new Runner(t1,l1);
            new Thread(r1).start();
            Runner r2=new Runner(t2,l2);
            new Thread(r2).start();
            Runner r3=new Runner(t3,l3);
            new Thread(r3).start();
            
            f.setLocation(200,200);
    f.pack();
    f.setVisible(true);
    }

    public static void main(String[] args){
      new WorldClock();
      /*
      String[] s=TimeZone.getAvailableIDs();
      for(int i=0;i<s.length;i++){
      System.out.println (s[i]);
      }
      */
      }


    }
    Runner.java
    import java.awt.*;
    import java.util.*;class Runner implements Runnable{
    TimeZone tz;
    Label l;

    public Runner(){

    }
    public Runner(TimeZone tz,Label l){
    this.tz=tz;
    this.l=l;
    }

    public void run(){
       while(true){
            Calendar c=Calendar.getInstance(tz);
        l.setText(c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND));
            try{
               Thread.sleep(1000);
            }catch(Exception e){
             e.printStackTrace();
            }
         }
      }
     
     
    }
      

  3.   

    请参考这段代码
    一段生产者和消费者的简单多线程代码
    http://www.java2000.net/viewthread.jsp?tid=581
      

  4.   


    //经典的生产者消费问题
    public class ProducerConsumer {
    public static void main(String[]args) {
    SyncStack ss = new SyncStack();
    Producer p = new Producer(ss);
    Consumer c = new Consumer(ss);

    new Thread(p).start();
    new Thread(c).start();

    }
    }class WoTou {
    int id;
    WoTou(int id) {
    this.id = id;
    }

    public String toString() {
    return "" + id;
    }
    }
    class SyncStack {
    WoTou[]wt = new WoTou[6];
    int index;

    public synchronized void push(WoTou w) {
      while(index == wt.length) {
       try {
       this.wait();
       }catch(InterruptedException i) {
       i.printStackTrace();
       break;
       }
       }
      
      this.notify();
    wt[index] = w;
    index++;
    }

    public synchronized WoTou pop() {
    while(index == 0) {
    try {
    this.wait();
    }catch(InterruptedException e) {
    e.printStackTrace();
    break;
    }

    }
    this.notify();
    index--;
    return wt[index];
    }
    }class Producer implements Runnable {
    SyncStack ss = null;

    Producer(SyncStack ss) {
    this.ss = ss;
    }

    public void run() {
    for(int i=0;i<20;i++) {
    WoTou wt = new WoTou(i);
    ss.push(wt);
    System.out.println("生产了:"+wt);

    try {
    Thread.sleep(200);
    }catch(InterruptedException ii) {
    ii.printStackTrace();
    }

    }
    }
    }class Consumer implements Runnable {
    SyncStack ss = null;
    Consumer(SyncStack ss) {
    this.ss = ss;
    }

    public void run() {
    for(int i=0;i<20;i++) {
    WoTou wt = ss.pop();
    System.out.println("消费了:"+wt);

    try{
    Thread.sleep(2000);
    }catch(InterruptedException iii) {
    iii.printStackTrace();
    }
    }
    }
    }