zoukankan      html  css  js  c++  java
  • 多线程按序打印1-100

    1.两个线程交替打印

    package Mult_thread;
    
    public class turnPrint {
        private volatile int flag = 0;
        private volatile int count = 1;
        public static void main(String[] args) {
            turnPrint t = new turnPrint();
            t.getThread();
        }
        public void getThread(){
            Thread t1 = new Thread(new Thread1());
            Thread t2 = new Thread(new Thread2());
            t1.start();
            t2.start();
        }
        public class Thread1 implements Runnable{
            public void run(){
                while(count<=100){
                    if(flag==0){
                        System.out.println(count);
                        count++;
                        flag = 1;
                    }
                }
            }
        }
        public class Thread2 implements Runnable{
            public void run(){
                while(count<=100){
                    if(flag==1){
                        System.out.println(count);
                        count++;
                        flag = 0;
                    }
                }
            }
    
    
        }
    }
    public class Main {
        volatile int flag=0;
        public static void main(String[] args) throws InterruptedException {
            Main thread = new Main();
            thread.runThread();
        }
        public void runThread() throws InterruptedException{
            Thread t1=new Thread(new Thread1());
            Thread t2=new Thread(new Thread2());
            t1.start();
            t2.start();
        }
        public class Thread1 implements Runnable{
            public void run() {
                int i=1;
                while(i<=99){
                    if(flag==0)
                    {
                        System.out.println("t1="+i+"flag="+flag);
                        i+=2;
                        flag=1;
                    }
                }
            }
        }
        public class Thread2 implements Runnable{
    
            public void run() {
                int i=2;
                while(i<=100){
                    if(flag==1)
                    {
                        System.out.println("t2="+i+"flag="+flag);
                        i+=2;
                        flag=0;
                    }
                }
            }
        }
    }
    View Code

    2.三个线程交替打印

    import java.util.concurrent.atomic.AtomicInteger;
    
    
    public class Main2 {
        private volatile int i=1;
        private Thread thread1,thread2,thread3;
        private volatile int flag=0;
    
        public static void main(String[] args) throws InterruptedException {
            Main2 thread = new Main2();
            thread.runThread();
        }
        public void runThread() throws InterruptedException{
            thread1=new Thread(new Thread1());
            thread2=new Thread(new Thread2());
            thread3=new Thread(new Thread3());
            thread1.start();
            thread2.start();
            thread3.start();
        }
        public class Thread1 implements Runnable{
    
            public void run() {
                while(i<100){
                    if(flag==0) {
                        System.out.println("t1="+i);
                        i++;
                        flag=1;
                    }
                }
            }
    
        }
    
        public class Thread2 implements Runnable{
    
            public void run() {
    
                while(i<100){
                    if(flag==1){
                        System.out.println("t2="+i);
                        i++;
                        flag=2;
                    }
                }
            }
    
        }
    
        public class Thread3 implements Runnable{
    
            public void run() {
    
                while(i<100){
                    if(flag==2){
                        System.out.println("t3="+i);
                        i++;
                        flag=0;
                    }
                }
            }
    
        }
    }
    View Code
  • 相关阅读:
    Amphiphilic Carbon Molecules [UVA
    2018宁夏邀请赛I题 bubble sort(思维题
    CF1198E Rectangle Painting 2(最小割 思维
    Problem : 这个题如果不是签到题 Asm.Def就女装(积性函数dp
    cogs2223. [SDOI2016 Round1] 生成魔咒(后缀数组 hash 二分 set
    cogs1709. [SPOJ 705] 不同的子串(后缀数组
    cogs249 最长公共子串(后缀数组 二分答案
    hdu2222 Keywords Search (AC自动机板子
    ccpc网赛 hdu6703 array(权值线段树
    ccpc网赛 hdu6705 path(队列模拟 贪心
  • 原文地址:https://www.cnblogs.com/jingpeng77/p/13531261.html
Copyright © 2011-2022 走看看