俩个线程交替打印i到n的多种实现方式

本文探讨了两种Java中实现线程交替打印1到n的策略:一是利用volatile关键字保证可见性;二是利用AtomicInteger类的原子操作特性确保线程安全。这两种方法都是多线程编程的有效实践。
摘要由CSDN通过智能技术生成

一、使用volatile关键字实现

public class Print10 {
    static volatile  int i = 0;

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while(i < 20){
                    synchronized (Print10.class){
                    	// 锁的是类锁,所以要释放类的锁
                        Print10.class.notify(); // 唤醒其他线程可以抢锁了
                        System.out.println(Thread.currentThread().getName() + "打印:"+ i++);

                        try {
                            Print10.class.wait(); // 让此线程等待,避免下次和其他线程抢锁
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };

        Thread t1 = new Thread(runnable,"t1");
        Thread t2 = new Thread(runnable,"t2");
        t1.start();
        t2.start();
    }
}
public class Print10 {
    static volatile  int state = 0;
    static volatile int i = 0;

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while(i < 20){
                        if(state % 2 == 0 && Thread.currentThread().getName().equals("t1")){
                            System.out.println(Thread.currentThread().getName() + "打印:"+ i);
                            state++;
                            i++;
                        }
                        if (state % 2 == 1 && Thread.currentThread().getName().equals("t2")){
                            System.out.println(Thread.currentThread().getName() + "打印:"+ i);
                            state++;
                            i++;
                        }
                }
            }
        };

        Thread t1 = new Thread(runnable,"t1");
        Thread t2 = new Thread(runnable,"t2");
        t1.start();
        t2.start();
    }
}

二、使用原子类实现 AtomicInteger

public class Print100 {
    static volatile AtomicInteger i  = new AtomicInteger(0);
    public static void main(String[] args) {
        Thread t1 = new Thread(new print(i),"t1");
        Thread t2 = new Thread(new print(i),"t2");
        t1.start();
        t2.start();
    }
}

class print implements  Runnable{
    volatile static AtomicInteger i;
    public print(AtomicInteger i) {
        this.i = i;
    }
    @Override
    public void run() {
        while(i.get() < 10) {
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + "打印了:" + i.incrementAndGet());
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值