package bytedance;import java.util.concurrent.locks.ReentrantLock;/*** @Description* @Author msli* @Date 2022年01月13日*/public class PrintThread {public static int count = 0;public static int threadSize = 3;private static ReentrantLock lock = new ReentrantLock();public static void main(String[] args) throws InterruptedException {Thread[] threads= new Thread[threadSize];for (int i = 0; i < threads.length; i++) {final int ii = i;threads[i] = new Thread( () -> {try {lock.lock();if (count % threadSize == ii) {System.out.println(String.format("第 %d 个线程[%s]开始打印:count = %d", ii, Thread.currentThread().getName(), count));count++;}} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}});}for (int i = 0; i < threads.length; i++) {threads[i].start();threads[i].join();}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。