单例模式/多线程打印1-200代码

1.手写单例模式

public class Single {
    private volatile static Single singleCase;

    private Single() {}

    public static Single getSingleCase(){
        if(singleCase == null){
            synchronized (Single.class){
                if(singleCase == null){
                    singleCase = new Single();
                }
            }
        }
        return singleCase;
    }
}

2.多线程实现输出1~100;
(1)三个线程依次轮流输出

public class Print200 {
    public static volatile int flag = 0;
    public static int num = 0;
    public static void main(String[] args){
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        Thread3 t3 = new Thread3();
        t1.start();
        t2.start();
        t3.start();
    }

    static class Thread1 extends Thread{
        @Override
        public void run() {
            while (num <= 100){
                if(flag == 0){
                    System.out.println(num);
                    num++;
                    flag = 1;
                }
            }
        }
    }

    static class Thread2 extends Thread{
        @Override
        public void run() {
            while (num <= 100){
                if(flag == 1){
                    System.out.println(num);
                    num++;
                    flag = 2;
                }
            }
        }
    }

    static class Thread3 extends Thread{
        @Override
        public void run() {
            while (num <= 100){
                if(flag == 2){
                    System.out.println(num);
                    num++;
                    flag = 0;
                }
            }
        }
    }
}

(2)通过让Thread重新run方法时引用自定义方法,其中使用了Lock锁。

public class Print2001 {
    //private int state = 0;
    private int num = 0;
    private Lock lock = new ReentrantLock();

    private Print2001(){
    }

    private void sou(int state, String name){
        if (num >= 100) return;
        while (num < 100){
            if(num % 3 == state){
                lock.lock();
                num++;
                System.out.println(name + num);
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        Print2001 p = new Print2001();
        new Thread(new Runnable() {
            @Override
            public void run() {
                p.sou(0, "A");
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                p.sou(1, "B");
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                p.sou(2, "C");
            }
        }).start();
    }
}

(3)乱序输出1-100,使用原子类

public class Print2002 {
    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) {
        Thread4 t1 = new Thread4();
        Thread4 t2 = new Thread4();
        Thread4 t3 = new Thread4();
        t1.start();
        t2.start();
        t3.start();
    }

    static class Thread4 extends Thread{
        @Override
        public void run() {
            while (atomicInteger.get() <= 100) {
                System.out.println(atomicInteger);
                atomicInteger.incrementAndGet();
            }
        }
    }
}

对上述进行修改:
实现顺序输出,还是使用原子类

public class Print2002 {
    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) {
        Thread4 t1 = new Thread4(0);
        Thread4 t2 = new Thread4(1);
        Thread4 t3 = new Thread4(2);
        t1.start();
        t2.start();
        t3.start();
    }

    static class Thread4 extends Thread{
        int state;
        public Thread4(int state){
            super();
            this.state = state;
        }
        @Override
        public void run() {
            while (atomicInteger.get() <= 100) {
                if(atomicInteger.get() % 3 == state){
                    System.out.println(atomicInteger);
                    atomicInteger.incrementAndGet();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值