A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zjm10zj 中级黑马   /  2013-3-23 09:19  /  1499 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 zjm10zj 于 2013-3-24 22:31 编辑

怎么对多个线程同步通信进行控制!使用那些方法!

点评

记得及时处理帖子哦,继续追问,或将分类改成【已解决】,谢谢  发表于 2013-3-24 07:49

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1 鼓励鼓励

查看全部评分

2 个回复

倒序浏览
在多线程并发执行的时候,可以使用wait()和notify()方法在多个线程间通信。  前提是必须是同步方法之间进行通信,必须使用锁对象调动wait()和notify()
使用。通信的代码必须写在同步代码中,必须使用锁对象来调用wait()和notify(),wait()方法可以控制当前线程等待, 直到其他线程调用notify()或者notifyAll()方法才被唤醒。
给你一个生产者消费者的例子:
/**
* 公共资源类
*/  
public class PublicResource {  
    private int number = 0;  
  
    /**
     * 增加公共资源
     */  
    public synchronized void increace() {  
        while (number != 0) {  
            try {  
                wait();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
        number++;  
        System.out.println(number);  
        notify();  
    }  
  
    /**
     * 减少公共资源
     */  
    public synchronized void decreace() {  
        while (number == 0) {  
            try {  
                wait();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
        number--;  
        System.out.println(number);  
        notify();  
    }  
}  
/**
* 生产者线程,负责生产公共资源
*/  
public class ProducerThread implements Runnable {  
    private PublicResource resource;  
  
    public ProducerThread(PublicResource resource) {  
        this.resource = resource;  
    }  
   
    public void run() {  
        for (int i = 0; i < 10; i++) {  
            try {  
                Thread.sleep((long) (Math.random() * 1000));  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
            resource.increace();  
        }  
    }  
}  
/**
* 消费者线程,负责消费公共资源
*/  
public class ConsumerThread implements Runnable {  
    private PublicResource resource;  
  
    public ConsumerThread(PublicResource resource) {  
        this.resource = resource;  
    }  
  
   
    public void run() {  
        for (int i = 0; i < 10; i++) {  
            try {  
                Thread.sleep((long) (Math.random() * 1000));  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
            resource.decreace();  
        }  
    }  
}  
public class ProducerConsumerTest {  
    public static void main(String[] args) {  
        PublicResource resource = new PublicResource();  
        new Thread(new ProducerThread(resource)).start();  
        new Thread(new ConsumerThread(resource)).start();  
        new Thread(new ProducerThread(resource)).start();  
        new Thread(new ConsumerThread(resource)).start();  
        new Thread(new ProducerThread(resource)).start();  
        new Thread(new ConsumerThread(resource)).start();  
    }  
}

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
多线程的同步控制与线程间的通信:可以用synchronized、wait()和notifyAll()完成以下情景
模拟3个人排队买票,每人买一张票。售票员(TicketSeller类)只有1张5元的钱,电影票5元一张。
张某拿着1张20元的人民币排在第一,李某拿着1张10元的人民币排在第二,王某拿着1张5元的人民币排在第三。
(提示:定义一个售票员TicketSeller类,属性包括5元钱张数fiveNumber、10元钱张数tenNumber和20元钱张数twentyNumber,方法为同步方法卖票sellTicket(int receiveMoney), 创建三个线程张某Zhang、李某Li和王某Wang,这三个线程共享一个售票员类对象。




class MyThread88 implements Runnable {
  int i=100;
  public synchronized void m1() throws InterruptedException {
    i=1000;
    Thread.sleep(5000);
    System.out.println("i"+"="+i);
  }
  public synchronized void m2() throws InterruptedException {
    Thread.sleep(2500);
      i=2000;
  }
  public void run() {
   try {
     m1();
   } catch (InterruptedException e) {
       e.printStackTrace();
     }
  }
}

public class TongBu {
  public static void main(String[] args) {
    MyThread88 k = new MyThread88();
    Thread t = new Thread(k);
    t.start();
    try {
      k.m2();
    } catch (InterruptedException e) {
        e.printStackTrace();
      }
    System.out.println(k.i);
  }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马