一道多线程题目的解决方案

开发 后端
在iteye上看到的一道多线程的题目,参考了一下网友的实现,那Eclipse调试通过,算是对JAVA5的并发库有个大致的了解,分享出来,欢迎园里的同学拍砖。

在iteye上看到的一道多线程的题目,参考了一下网友的实现,那Eclipse调试通过,算是对JAVA5的并发库有个大致的了解,分享出来,欢迎园里的同学拍砖。

题目:

要求用三个线程,按顺序打印1,2,3,4,5.... 71,72,73,74, 75.

线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到线程3打印到75。

分析:感觉出题人是要考察一下你是否能够很好的控制多线程,让他们有序的进行。

1、线程池:3个线程,需要使用并发库的线程池

2、锁(lcok):在打印的时候,只允许一个线程进入,其他的线程等待

下面的主要的代码:

import java.util.HashMap;  
import java.util.Map;  
import java.util.concurrent.CountDownLatch;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
import java.util.concurrent.locks.Condition;  
import java.util.concurrent.locks.Lock;  
import java.util.concurrent.locks.ReentrantLock;  
 
public class NumberPrinter {  
 
    private Lock lock = new ReentrantLock();  
 
    private Condition c1 = lock.newCondition();  
    private Condition c2 = lock.newCondition();  
    private Condition c3 = lock.newCondition();  
 
    private Map<Integer, Condition> condtionContext =   
        new HashMap<Integer, Condition>();  
 
    public NumberPrinter() {  
        condtionContext.put(Integer.valueOf(0), c1);  
        condtionContext.put(Integer.valueOf(1), c2);  
        condtionContext.put(Integer.valueOf(2), c3);  
    }  
      
    private int count = 0;     
      
    public void print(int id) {  
        lock.lock();  
        try {  
            while(count*5 < 75) {  
                int curID = calcID();  
                if (id == curID) {  
                    for (int i = 1; i<=5; i++) {  
                        System.out.print(count*5 +i+ ",");  
                    }  
                    System.out.println();  
                    count++;  
                    int nextID = calcID();  
                    Condition nextCondition = condtionContext.get(  
                            Integer.valueOf(nextID));  
                    //通知下一线程  
                    nextCondition.signal();  
                } else {  
                    Condition condition = condtionContext.get(  
                            Integer.valueOf(id));  
                    condition.await();  
                }  
            }  
            //通知线程结束  
            for(Condition c : condtionContext.values()) {  
                c.signal();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            lock.unlock();  
        }  
    }  
      
    private int calcID() {  
        // TODO Auto-generated method stub  
        return count % 3;  
    }  
 
 
    /**  
     * @param args  
     */ 
    public static void main(String[] args) {  
        ExecutorService executor = Executors.newFixedThreadPool(3);  
        final CountDownLatch latch = new CountDownLatch(1);     
        final NumberPrinter printer = new NumberPrinter();   
        for (int i = 0; i < 3; i++) {     
            final int id = i;  
            executor.submit(new Runnable() {  
                @Override 
                public void run() {  
                    // TODO Auto-generated method stub  
                    try {  
                        latch.await();  
                    } catch (InterruptedException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                    printer.print(id);  
                }  
            });  
        }  
        System.out.println("三个任务开始顺序打印数字。。。。。。");   
        latch.countDown();  
        executor.shutdown();  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.

原文链接:http://www.cnblogs.com/sodmecai/archive/2012/05/17/2506230.html

【编辑推荐】

  1. Java的Comparable接口的一个陷阱
  2. Java程序设计:图形与多媒体处理
  3. 详解Java类的生命周期
  4. Java理论与实践: Web层的状态复制
  5. Apache CXF实战之三:传输Java对象
责任编辑:林师授 来源: 在程序的路上博客
相关推荐

2009-09-14 19:39:14

批量线程同步

2009-07-15 17:09:32

Swing线程

2021-11-10 07:47:49

Python源码代码

2018-03-13 16:04:45

Promise执行顺序

2025-01-07 08:20:00

2024-03-18 13:32:11

2009-08-11 10:12:07

C#算法

2025-03-03 01:25:00

SpringAOP日志

2009-03-18 09:26:23

Winform多线程C#

2015-06-17 11:18:08

C#多线程基础练习题

2013-04-17 16:03:40

华为IT解决方案巡展

2021-03-02 11:29:50

算法算法分析前端

2013-04-17 15:00:38

华为巡展

2012-05-27 16:21:31

IDC华为

2018-12-03 12:17:27

Semptian解决方案

2018-12-03 11:59:42

Inventec解决方案

2018-12-03 12:13:21

Mellanox解决方案

2018-12-03 12:26:30

YADRO解决方案

2016-03-13 19:23:15

2010-02-24 14:55:50

思科
点赞
收藏

51CTO技术栈公众号