有三个线程名字分别是A、B、C,每个线程只能打印自己的名字,在屏幕上顺序打印 ABC,打印10次...

一个比较简单的例子

对公共资源加锁,以阻塞其它线程。
用一个全局变量(3个线程都可访问的公共变量)控制状态

/**
 * 有三个线程名字分别是A、B、C,每个线程只能打印自己的名字,在屏幕上顺序打印 ABC,打印10次
 * 
 * @author chenhening
 * @date 2017年3月16日
 */
public class MyABC extends Thread {
    private static Object o = new Object();// 对象锁,必须是static
    private static int count = 0;// 控制输出哪个字母
    private char ID;// 字母
    private int id;// 字母对应的数字
    private int num = 0;// 打印次数
    public MyABC(int id, char ID) {
        this.id = id;
        this.ID = ID;
    }

    @Override
    public void run() {
        synchronized (o) {
            while (num < 10) {
                if (count % 3 == id) {
                    System.out.print(ID);
                    ++count;
                    ++num;

                    // 唤醒所有等待线程
                    o.notifyAll();
                } else {
                    try {
                        // 如果不满足条件,则阻塞等待
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        (new MyABC(0, 'A')).start();
        (new MyABC(1, 'B')).start();
        (new MyABC(2, 'C')).start();
    }
}

 

第二种方法:

/**
 * 有三个线程名字分别是A、B、C,每个线程只能打印自己的名字,在屏幕上顺序打印 ABC,打印10次
 * 
 * @author chenhening
 * @date 2017年3月16日
 */
public class SwitchStatus extends Thread {
    private static String currentThread = "A";// 初始为A
    private static Object lock = new Object();// 作为锁
    private String name = "";
    private int count = 10;// 打印次数

    public SwitchStatus(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (count > 0) {
            synchronized (lock) {
                if (currentThread.equals(this.name)) {
                    System.out.print(name);
                    count--;
                if (currentThread.equals("A")) {
                    currentThread = "B";
                } else if (currentThread.equals("B")) {
                    currentThread = "C";
                } else if (currentThread.equals("C")) {
                    currentThread = "A";
                    System.out.println();
                }
                }
            }
        }
    }

    public static void main(String[] args) {
        (new SwitchStatus("A")).start();
        (new SwitchStatus("B")).start();
        (new SwitchStatus("C")).start();
    }
}

 

第三种:

/**
 * 有三个线程名字分别是A、B、C,每个线程只能打印自己的名字,在屏幕上顺序打印 ABC,打印10次
 * 
 * @author chenhening
 * @date 2017年3月16日
 */
public class SleepExample extends Thread {

    private static int currentCount = 0;

    public SleepExample(String name) {
        // 设置线程的名称
        this.setName(name);
    }

    @Override
    public void run() {
        while (currentCount < 30) {// 10遍*3个字母
            switch (currentCount % 3) {// 除以3求余数
                case 0:
                    if ("A".equals(getName())) {
                        System.out.print(getName());
                        currentCount++;
                    }
                    break;
                case 1:
                    if ("B".equals(getName())) {
                        System.out.print(getName());
                        currentCount++;
                    }
                    break;
                case 2:
                    if ("C".equals(getName())) {
                        System.out.print(getName());
                        currentCount++;
                    }
                    break;
            }
        }
    }

    public static void main(String[] args) {
        // 线程启动顺序无关
        new SleepExample("A").start();
        new SleepExample("B").start();
        new SleepExample("C").start();
    }

}

 

转载于:https://www.cnblogs.com/chn58/p/6558829.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值