多线程顺序打印

前言:

      下面的代码是参考网址:http://love3400wind.blog.163.com/blog/static/796308012013117102941831/ , http://blog.csdn.net/liu251/article/details/6227763 做了一点点的改动。

      感谢两位作者!

题目:

有A,B,C三个线程, A线程输出A, B线程输出B, C线程输出C

要求, 同时启动三个线程, 按顺序输出ABC, 循环10次

 

核心要素:

     1,工作线程对象之间的关联。

     2,工作线程的状态修改(前一个线程对象有能力修改后一个线程对象的状态)。

 

示例:

package org.wit.ff.thread;

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;

/**
 * 
 * @author F.Fang
 * 
 */
public class ABCDemo {

    private Lock lock = new ReentrantLock();// 通过JDK5中的锁来保证线程的访问的互斥
    private Condition condition = lock.newCondition();// 线程协作

    public static void main(String[] args) {
        ABCDemo abc = new ABCDemo();

        // 使用循环可以不局限于只有3个线程 交替, 任意多个都可以.
        PrintThread a = abc.new PrintThread("A", true);
        PrintThread b = abc.new PrintThread("B", false);
        PrintThread c = abc.new PrintThread("C", false);

        a.setNext(b);
        b.setNext(c);
        c.setNext(a);

        ExecutorService executor = Executors.newFixedThreadPool(3);// 通过线程池执行
        for (int i = 0; i < 3; i++) {
            executor.execute(a);
            executor.execute(b);
            executor.execute(c);
        }
        executor.shutdown();//关闭线程池
    }

    class PrintThread implements Runnable {
        private String name;

        private PrintThread next;

        private boolean execute;

        public PrintThread(String name, boolean execute) {
            this.name = name;
            this.execute = execute;
        }

        public PrintThread(String name, PrintThread next, boolean execute) {
            this.name = name;
            this.next = next;
            this.execute = execute;
        }

        public void run() {
            lock.lock();
            try {
                while (true) {

                    if (execute) {
                        // 执行当前业务
                        print();
                        // 当前任务执行完成后 改变执行状态为false
                        execute = false;
                        // 将下一个相邻任务状态设置为true
                        next.setExecute(true);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // 通知其它任务.
                        condition.signalAll();
                        break;
                    } else {
                        try {
                            // 若非执行状态 即等待.
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } finally {
                lock.unlock();
            }
        }

        public void print() {
            System.out.println(name);
        }

        public void setExecute(boolean execute) {
            this.execute = execute;
        }

        public void setNext(PrintThread next) {
            this.next = next;
        }

    }

}

 

 

 

      

转载于:https://www.cnblogs.com/fangfan/p/3957665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值