欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java多线程交替打印

程序员文章站 2022-03-23 22:11:05
import org.junit.Test;import java.util.concurrent.*;public class TestThreadLocal { Object o = new Object(); CountDownLatch c=new CountDownLatch(2); @Test public void vvvvvvvv() throws InterruptedException { Thread t1 = new Thread...

notify+wait实现

import org.junit.Test;
import java.util.concurrent.*;

public class TestThreadLocal {
    Object o = new Object();
    CountDownLatch c=new CountDownLatch(2);
    @Test
    public void vvvvvvvv() throws InterruptedException {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 26; i++) {
                    synchronized (o) {
                        System.out.print((char) (65 + i));
                        o.notify();
                        try {
                            if(i<25)o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                c.countDown();
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 26; i++) {
                    synchronized (o) {
                        System.out.print(1 + i);
                        o.notify();
                        try {
                            if(i<25)o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                c.countDown();
            }
        };
        t1.start();
        t2.start();
        //t1.join();
        //t2.join();
        c.await();
    }
}

A1B2C3D4E5F6G7H8I9J10K11L12M13N14O15P16Q17R18S19T20U21V22W23X24Y25Z26
t2可能先执行,notify(只随机唤醒一个 wait 线程)改成notifyAll(唤醒所有 wait 线程)更好。
这两个方法只唤醒,被唤醒的线程处于runnable状态。想交替执行,需要负责唤醒的线程自己阻塞。

LockSupport实现

import org.junit.Test;
import java.util.concurrent.*;
import java.util.concurrent.locks.LockSupport;

public class TestThreadLocal {
    CountDownLatch c=new CountDownLatch(2);
    Thread t1 ,t2;
    @Test
    public void vvvvvvvv() throws InterruptedException {
        t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 26; i++) {
                    System.out.print((char) (65 + i));
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
                c.countDown();
            }
        };
        t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 26; i++) {
                    LockSupport.park();
                    System.out.print(1+i);
                    LockSupport.unpark(t1);
                }
                c.countDown();
            }
        };
        t1.start();
        t2.start();
        //t1.join();
        //t2.join();
        c.await();
    }
}

unpark调用时,如果当前线程还未进入park,则许可为true;
park调用时,判断许可是否为true,如果是true,则继续往下执行;如果是false,则等待,直到许可为true。
如果t2先执行,会park让t1先执行。如果t1先执行,打印后unpark t2,保证两个线程不会同时阻塞。
如果t2的LockSupport.park()和System.out.print(1+i)交换位置,可能出现t2连续打印两次的情况。
即t1打印后,执行unpartk(t2)前t2打印一次,然后t1 unpark t2后t2抢在t1前再打印一次。

本文地址:https://blog.csdn.net/weixin_43292547/article/details/109955789

相关标签: java相关