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();
    }
}

猜你喜欢

转载自blog.csdn.net/Devil_Net/article/details/80349127
今日推荐