文档章节

多线程交替输出1234

o
 osc_4nmshwhm
发布于 2018/08/06 17:22
字数 616
阅读 0
收藏 0

精选30+云产品,助力企业轻松上云!>>>

1、synchronized

package threadT;

public class TestPrint {
    static int i = 1;

    public static void main(String[] args) throws Exception {
        final Object obj = new Object();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        synchronized (obj) {
                            Thread.sleep(1000);
                            obj.notifyAll();
                            obj.wait();
                            if (i == 1)
                                System.out.println(i++ % 3);
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        synchronized (obj) {
                            Thread.sleep(1000);
                            obj.notifyAll();
                            obj.wait();
                            if (i == 2)
                                System.out.println(i++ % 3);
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        Thread t3 = new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        synchronized (obj) {
                            Thread.sleep(1000);
                            obj.notifyAll();
                            obj.wait();
                            if (i == 3) {
                                System.out.println(3);
                                i = 1;
                            }
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    };
}

2、ReentrantLock

package threadT;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestPrint {
    private static Lock lock = new ReentrantLock();
    private static int state = 0;

    static class First extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.lock();
                if (state % 4 == 0) {
                    System.out.println("1");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    static class Second extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.lock();
                if (state % 4 == 1) {
                    System.out.println("2");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    static class Third extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.lock();
                if (state % 4 == 2) {
                    System.out.println("3");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    static class Forth extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.lock();
                if (state % 4 == 3) {
                    System.out.println("4");
                    state++;
                }
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        First first = new First();
        Second second = new Second();
        Third third = new Third();
        Forth forth = new Forth();
        first.start();
        second.start();
        third.start();
        forth.start();
    }
}

3、Semaphore

package threadT;

import java.util.concurrent.Semaphore;

public class TestPrint {
    public static Semaphore sem1;
    public static Semaphore sem2;
    public static Semaphore sem3;
    public static Semaphore sem4;

    static class FirstThread extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(1000);
                    sem1.acquire();
                    System.out.println("1");
                    sem2.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class SecondThread extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(1000);
                    sem2.acquire();
                    System.out.println("2");
                    sem3.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class ThirdThread extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(1000);
                    sem3.acquire();
                    System.out.println("3");
                    sem4.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class ForthThread extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(1000);
                    sem4.acquire();
                    System.out.println("4");
                    sem1.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        sem1 = new Semaphore(1);
        sem2 = new Semaphore(1);
        sem3 = new Semaphore(1);
        sem4 = new Semaphore(1);
        try {
            // 不要有sem1.acquire()
            sem2.acquire();
            sem3.acquire();
            sem4.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new FirstThread().start();
        new SecondThread().start();
        new ThirdThread().start();
        new ForthThread().start();

    }
}

4、volatile

package threadT;

public class TestPrint {
    volatile static int state = 0;

    static class First extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (state % 4 == 0) {
                    System.out.println("1");
                    state++;
                }
            }
        }
    }

    static class Second extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (state % 4 == 1) {
                    System.out.println("2");
                    state++;
                }
            }
        }
    }

    static class Third extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (state % 4 == 2) {
                    System.out.println("3");
                    state++;
                }
            }
        }
    }

    static class Forth extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (state % 4 == 3) {
                    System.out.println("4");
                    state++;
                }
            }
        }
    }

    public static void main(String[] args) {
        First first = new First();
        Second second = new Second();
        Third third = new Third();
        Forth forth = new Forth();
        first.start();
        second.start();
        third.start();
        forth.start();
    }
}

 

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

格式编号始终显示2个小数位 - Format number to always show 2 decimal places

问题: I would like to format my numbers to always display 2 decimal places, rounding where applicable. 我想将数字格式化为始终显示2个小数位,并在适用的情况下四舍五入。 Examples...

富含淀粉
45分钟前
22
0
Docker可视化工具Portainer

1 前言 从没想到Docker也有可视化的工具,因为它的命令还是非常清晰简单的。无聊搜了一下,原来已经有很多Docker可视化工具了。如DockerUI、Shipyard、Rancher、Portainer等。查看对比了一番...

南瓜慢说
47分钟前
20
0
日志系统新贵 Loki,真香!!

最近,在对公司容器云的日志方案进行设计的时候,发现主流的ELK或者EFK比较重,再加上现阶段对于ES复杂的搜索功能很多都用不上最终选择了Grafana开源的Loki日志系统,下面介绍下Loki的背景。...

庞陆阳
今天
14
0
jQuery获取select onChange的值 - jQuery get value of select onChange

问题: I was under the impression that I could get the value of a select input by doing this $(this).val(); 我的印象是我可以通过执行$(this).val();来获取选择输入的值$(this).val()......

javail
今天
13
0
道翰天琼解密宇宙信息大脑三者最核心奥秘,破解认知智能基础理论(群聊形式)

三体论是探索研究宇宙,信息和人类大脑三者关系的理论体系。是认知智能的奠基理论体系之一。宇宙和信息,信息和人类大脑,人类大脑和宇宙,三者之间存在着某种未被完全揭示的奥秘。三体论的核...

jackli2020
今天
15
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部