Devil_Net的博客

Devil_Net的博客

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();
    }
}
阅读更多
版权声明: https://blog.csdn.net/Devil_Net/article/details/80349127
个人分类: java
想对作者说点什么? 我来说一句

Java多线程练习题

2008年11月21日 39KB 下载

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭