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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王晓龙 初级黑马   /  2012-8-3 22:36  /  1370 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

开启2条线程, 轮流执行打印操作, 执行结果如下:
       
        线程1: 1
        线程1: 2
        线程1: 3
       
        线程2: 4
        线程2: 5
        线程2: 6
       
        线程1: 7
        线程1: 8
        线程1: 9
       
        线程2: 10
        线程2: 11
        线程2: 12
       
        线程1: 13
        线程1: 14
        线程1: 15
       
        线程2: 16
        线程2: 17
        线程2: 18
*/

5 个回复

倒序浏览
上面打错啦 问题下面发出来
回复 使用道具 举报
开启3条线程, 轮流执行打印操作, 执行结果如下:
       
        线程1: 1
        线程1: 2
        线程1: 3
       
        线程2: 4
        线程2: 5
        线程2: 6
       
        线程3: 7
        线程3: 8
        线程3: 9
       
        线程1: 10
        线程1: 11
        线程1: 12
       
        线程2: 13
        线程2: 14
        线程2: 15
       
        线程3: 16
        线程3: 17
        线程3: 18

        线程1: 19
        线程1: 20
        线程1: 21
       
        线程2: 22
        线程2: 23
        线程2: 24
       
        线程3: 25
        线程3: 26
        线程3: 27
这个怎么做 我会双线程的但3线程就不行 怎么弄?

评分

参与人数 1技术分 +1 收起 理由
包晗 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 刘润辰 于 2012-8-4 10:41 编辑

这个好办!判断一下就行。然后需要实现LOCK接口,JDK升级已经把这个问题解决简单话。

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Workkk {
        public static void main(String[] args) {

                final HomeWorkee h = new HomeWorkee();
                new Thread() {
                        public void run() {
                                for (int i = 0; i < 3; i++)
                                        try {
                                                h.haha();
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                        }
                }.start();

                new Thread() {
                        public void run() {
                                for (int i = 0; i < 3; i++)
                                        try {
                                                h.hah();
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                        }
                }.start();
               
                new Thread() {
                        public void run() {
                                for (int i = 0; i < 3; i++)
                                        try {
                                                h.ha();
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                        }
                }.start();
        }
        
}

class HomeWorkee {
        private int x = 1;
       private int flag = 3; //标记为3,只能最先执行线程1

        
       private Lock lock = new ReentrantLock(); // 实现锁的接口
        /**
         * 定义3个Condition
         */
        private Condition condition_1 = lock.newCondition();
        private Condition condition_2 = lock.newCondition();
        private Condition condition_3 = lock.newCondition();
        
        public  void hah()throws InterruptedException {
                lock.lock();
                try {
                if (flag == 1 ) {
                        
                        condition_1.await();//线程1等待
                }

                System.out.println("线程1:" + x++);
                System.out.println("线程1:" + x++);
                System.out.println("线程1:" + x++);
                System.out.println();

                flag = 1;
                condition_2.signal();//唤醒线程2
                }finally{
                        lock.unlock();
                }
        }
        
        public  void haha() throws InterruptedException{
                lock.lock();
                try {
                if (flag == 2 || flag == 3) {//flag初始值为3抢到资源也不会执行
                        condition_2.await();
                }
                System.out.println("线程2:" + x++);
                System.out.println("线程2:" + x++);
                System.out.println("线程2:" + x++);
                System.out.println();
                flag = 2;//置为2
                condition_3.signal();//唤醒线程3

                } finally {
                        lock.unlock();
                }
        }
        
        public  void ha() throws InterruptedException {
                lock.lock();
                try {
                if (flag == 3 || flag == 1) {
                        condition_3.await();
               
                }

                System.out.println("线程3:" + x++);
                System.out.println("线程3:" + x++);
                System.out.println("线程3:" + x++);
                System.out.println();

                flag = 3;
                condition_1.signal();//唤醒线程1
                }finally{
                        lock.unlock();
                }
        }

6666666666666.png (20.8 KB, 下载次数: 12)

6666666666666.png

评分

参与人数 1技术分 +1 收起 理由
包晗 + 1 鼓励一下

查看全部评分

回复 使用道具 举报
呵呵 ls 受教啦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马