千家信息网

如何实现一个多线程

发表于:2025-01-16 作者:千家信息网编辑
千家信息网最后更新 2025年01月16日,本篇文章为大家展示了如何实现一个多线程,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。传统线程测试:* 子线程循环10次,接着主线程循环100次,接着又回到子线
千家信息网最后更新 2025年01月16日如何实现一个多线程

本篇文章为大家展示了如何实现一个多线程,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

传统线程测试:* 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100次,如此循环50次,请写出程序。

一、传统方法(隐式锁)实现

package com.lau.javabase.lock;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * 传统线程测试: * 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100次,如此循环50次,请写出程序。 */public class TraditionThreadExerciseTest {    volatile Boolean executeInTurn = true;    public synchronized void subTask(int times) throws Exception {        while(!executeInTurn){            this.wait();        }        for(int i = 1; i <= 10; i++){            System.out.println("子任务内部运行第" + i + "次," + "总第" + times + "次");        }        executeInTurn = false;        this.notify();    }    public synchronized void mainTask(int times) throws Exception {        while(executeInTurn){            this.wait();        }        for(int i = 1; i <= 100; i++){            System.out.println("主任务内部运行第" + i + "次," + "总第" + times + "次");        }        executeInTurn = true;        this.notify();    }    /**     * @Description:特别注意:不能将主线程和子线程循环50次的调用写在一个for循环里,     * 因为这样会导致后面的跑到前面去,引发错序打印问题     * @param args     * @throws InterruptedException     */    public static void main(String[] args) throws InterruptedException {        TraditionThreadExerciseTest test = new TraditionThreadExerciseTest();        ExecutorService threadPool = Executors.newFixedThreadPool(4);        threadPool.submit(() -> {            try {                for(int i = 1; i <= 50; i++) {                    test.subTask(i);                }            } catch (Exception e) {                e.printStackTrace();            }        }, "subThread");        threadPool.submit(() -> {            try {                for(int i = 1; i <= 50; i++) {                    test.mainTask(i);                }            } catch (Exception e) {                e.printStackTrace();            }        }, "mainThread");        threadPool.shutdown();    }}

二、显示锁实现

package com.lau.javabase.lock;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 传统线程测试: * 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100次,如此循环50次,请写出程序。 */public class TraditionThreadLockTest {    volatile Boolean executeInTurn = true;    Lock lock = new ReentrantLock();    Condition condition = lock.newCondition();    public void subTask(int times) throws Exception {        try{            lock.lock();            while(!executeInTurn){                condition.await();            }            for(int i = 1; i <= 10; i++){                System.out.println("子任务内部运行第" + i + "次," + "总第" + times + "次");            }            executeInTurn = false;            condition.signal();        }        finally {            lock.unlock();        }    }    public void mainTask(int times) throws Exception {        try{            lock.lock();            while(executeInTurn){                condition.await();            }            for(int i = 1; i <= 100; i++){                System.out.println("主任务内部运行第" + i + "次," + "总第" + times + "次");            }            executeInTurn = true;            condition.signal();        }        finally {            lock.unlock();        }    }    /**     * @Description:特别注意:不能将主线程和子线程循环50次的调用写在一个for循环里,     * 因为这样会导致后面的跑到前面去,引发错序打印问题     * @param args     * @throws InterruptedException     */    public static void main(String[] args) throws InterruptedException {        TraditionThreadLockTest test = new TraditionThreadLockTest();        ExecutorService threadPool = Executors.newFixedThreadPool(4);        threadPool.submit(() -> {            try {                for(int i = 1; i <= 50; i++) {                    test.subTask(i);                }            } catch (Exception e) {                e.printStackTrace();            }        }, "subThread");        threadPool.submit(() -> {            try {                for(int i = 1; i <= 50; i++) {                    test.mainTask(i);                }            } catch (Exception e) {                e.printStackTrace();            }        }, "mainThread");        threadPool.shutdown();    }}

上述内容就是如何实现一个多线程,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0