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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘宣超 中级黑马   /  2014-9-12 21:41  /  1011 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


线程通讯问题
创建两个线程  分别依次给数组里面随机存放int数据
要求两个线程轮流存放各存放3次。

自己做了半天做成这样,也不知道还思路对不对。

求解答

import java.awt.Robot;
import java.util.Random;

public class Demo4{
        final static shareArray1 sh = new shareArray1();
        public static void main(String[] args) {
                new Thread(new Runnable() {
                       
                        @Override
                        public void run() {
                                for(int i = 0; i<3 ; i++){
                                       
                                        try {
                                                sh.Thread1(new Random().nextInt());
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                                }                       
                        }
                }).start();
                new Thread(new Runnable() {
                       
                        @Override
                        public void run() {
                                for(int i = 0; i<3;i++){
                                        try {
                                                sh.Thread1(new Random().nextInt());
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                                }
                               
                        }
                }).start();
        }
}

class shareArray1{
        private static int[] sum = new int[6];
        private static int n=0;
        private static boolean badge = true;
        public synchronized void Thread1(int i) throws InterruptedException{
                while(!badge)
                        this.wait();
       
                        sum[n] = i;
                        System.out.print(Thread.currentThread().getName());
                        System.out.println(sum[n]);
                        n++;       
                Thread.sleep(3000);
                badge = false;
                this.notifyAll();
       
        }
       
        public synchronized void Thread2(int i) throws InterruptedException{
                while(badge)
                        this.wait();

                        sum[n] = i;
                        System.out.print(Thread.currentThread().getName());
                        System.out.println(sum[n]);
                        n++;
                Thread.sleep(3000);
                badge = true;
                this.notifyAll();
        }
}

3 个回复

倒序浏览
不注释好难看···
回复 使用道具 举报
    匿名内部部看着就不舒服 0.0  还不如直接定义个类方便阅读
回复 使用道具 举报
这个就是售票模型
线程对共享数据对象加锁,一个线程执行一次存入操作,使用加锁对象的notify()唤醒另一个线程,然后使用加锁对象的wait()方法使当前线程陷入沉睡
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马