博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程交替输出1234
阅读量:4950 次
发布时间:2019-06-11

本文共 9390 字,大约阅读时间需要 31 分钟。

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

 

转载于:https://www.cnblogs.com/wujf/p/9431451.html

你可能感兴趣的文章
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
MapReduce 重要组件——Recordreader组件 [转]
查看>>
webdriver api
查看>>
转载-FileZilla Server源码分析(1)
查看>>
apache 实现图标缓存客户端
查看>>
MediaWiki左侧导航栏通过特殊页面就可以设置。
查看>>
html基础之DOM操作
查看>>
几种图表库
查看>>
揭秘:黑客必备的Kali Linux是什么,有哪些弊端?
查看>>
linux系统的远程控制方法——学神IT教育
查看>>
springboot+mybatis报错Invalid bound statement (not found)
查看>>
Linux环境下SolrCloud集群环境搭建关键步骤
查看>>
P3565 [POI2014]HOT-Hotels
查看>>
UVa11078:Open Credit System
查看>>
MongoDB的简单使用
查看>>
git clone 遇到的问题
查看>>
hdfs 命令使用
查看>>