JAVA多线程初识(7)---一个编程题

使用线程同步与等待机制打印下边
*Thread-0#Thread-1@Thread-2
*Thread-0#Thread-1@Thread-2
*Thread-0#Thread-1@Thread-2
Thread-0#Thread-1@Thread-2
。。。。

package javaThread;

class MyThread {
    private Object lock = new Object();
    private int flag;    //flag表示打印次数
    private int count;   //count表示循环次数

    public MyThread(int count) {
        super();
        this.count = count;
    }

    public void fun() {
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < count; i++) {
                    synchronized (lock) {
                        if (Thread.currentThread().getName().equals("Thread-0") && (flag % 3 == 0)) {
                        //调用线程的名字作比较如果相等则可以输出对应的标记符号
                            System.out.print("*Thread-0");
                            flag++;
                            lock.notifyAll();
                        } else {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < count; i++) {
                    synchronized (lock) {
                        if (Thread.currentThread().getName().equals("Thread-1") && (flag % 3 == 1)) {
                            System.out.print("@Thread-1");
                            flag++;
                            lock.notifyAll();
                        } else {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }
        });

        Thread thread3 = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < count; i++) {
                    synchronized (lock) {
                        if (Thread.currentThread().getName().equals("Thread-2") && (flag % 3 == 2)) {
                            System.out.println("#Thread-2");
                            flag++;
                            lock.notifyAll();
                        } else {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();
    }

}

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

        MyThread myThread = new MyThread(333);
        myThread.fun();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值