演示线程池的执行过程

public class ThreadPoolSelfDefinitionTest {

    private static List<Object> objects = new Vector<>();

    public static void main(String[] args) {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(
                1,
                2,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(2),
                namedThreadFactory,
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        System.out.println("r:" + r.toString() + ",executor:" + executor.toString());
                    }
                }
        );

//        singleThreadPool.execute(() -> System.out.println(Thread.currentThread().getName()));

        singleThreadPool.execute(() -> logMetric());
        singleThreadPool.execute(() -> logMetric2());
        singleThreadPool.execute(() -> logMetric3());
        singleThreadPool.execute(() -> logMetric4());
        singleThreadPool.execute(() -> logMetric5());
        singleThreadPool.execute(() -> logMetric6());
        //放入6个任务,第1个在执行(核心线程数),第2、3个在队里等待处理(workQueue),第4个任务因为队列满了会尝试新起一个线程处理,剩下2个因为超过最大线程数和最大队列了被直接拒绝掉。

        singleThreadPool.shutdown();
    }

    private static void logMetric() {
        System.out.println(Thread.currentThread().getName() + ":logMetric1");
        while (objects.size() != 10000000) {
            objects.add(new Object());
        }
        System.out.println(Thread.currentThread().getName() + ": objects 1:" + objects.size());
    }

    private static void logMetric2() {
        System.out.println(Thread.currentThread().getName() + ":logMetric2");
        while (objects.size() != 20000000) {
            objects.add(new Object());
        }
        System.out.println(Thread.currentThread().getName() + ": objects 2:" + objects.size());
    }

    private static void logMetric3() {
        System.out.println(Thread.currentThread().getName() + ":logMetric3");
        while (objects.size() != 30000000) {
            objects.add(new Object());
        }
        System.out.println(Thread.currentThread().getName() + ": objects 3:" + objects.size());
    }

    private static void logMetric4() {
        System.out.println(Thread.currentThread().getName() + ":logMetric4");
        while (objects.size() != 10000000) {
            objects.add(new Object());
        }
        System.out.println(Thread.currentThread().getName() + ": objects 1:" + objects.size());
    }

    private static void logMetric5() {
        System.out.println(Thread.currentThread().getName() + ":logMetric5");
        while (objects.size() != 20000000) {
            objects.add(new Object());
        }
        System.out.println(Thread.currentThread().getName() + ": objects 2:" + objects.size());
    }

    private static void logMetric6() {
        System.out.println(Thread.currentThread().getName() + ":logMetric6");
        while (objects.size() != 30000000) {
            objects.add(new Object());
        }
        System.out.println(Thread.currentThread().getName() + ": objects 3:" + objects.size());
    }

}

下面可能的几种结果:

demo-pool-0:logMetric1
demo-pool-1:logMetric4
r:com.ting.thread.pool.ThreadPoolSelfDefinitionTest$$Lambda$5/1567581361@32a1bec0,executor:java.util.concurrent.ThreadPoolExecutor@22927a81[Running, pool size = 2, active threads = 2, queued tasks = 2, completed tasks = 0]
r:com.ting.thread.pool.ThreadPoolSelfDefinitionTest$$Lambda$6/2027961269@5e8c92f4,executor:java.util.concurrent.ThreadPoolExecutor@22927a81[Running, pool size = 2, active threads = 2, queued tasks = 2, completed tasks = 0]
demo-pool-1: objects 1:10002239
demo-pool-1:logMetric2
demo-pool-1: objects 2:20002186
demo-pool-1:logMetric3
demo-pool-1: objects 3:30003928
demo-pool-0:logMetric1
demo-pool-1:logMetric4
r:com.ting.thread.pool.ThreadPoolSelfDefinitionTest$$Lambda$5/1567581361@32a1bec0,executor:java.util.concurrent.ThreadPoolExecutor@22927a81[Running, pool size = 2, active threads = 2, queued tasks = 2, completed tasks = 0]
r:com.ting.thread.pool.ThreadPoolSelfDefinitionTest$$Lambda$6/2027961269@5e8c92f4,executor:java.util.concurrent.ThreadPoolExecutor@22927a81[Running, pool size = 2, active threads = 2, queued tasks = 2, completed tasks = 0]
demo-pool-0: objects 1:10002328
demo-pool-0:logMetric2
demo-pool-0: objects 2:20002170
demo-pool-0:logMetric3
demo-pool-0:logMetric1
demo-pool-1:logMetric4
r:com.ting.thread.pool.ThreadPoolSelfDefinitionTest$$Lambda$5/1567581361@32a1bec0,executor:java.util.concurrent.ThreadPoolExecutor@22927a81[Running, pool size = 2, active threads = 2, queued tasks = 2, completed tasks = 0]
r:com.ting.thread.pool.ThreadPoolSelfDefinitionTest$$Lambda$6/2027961269@5e8c92f4,executor:java.util.concurrent.ThreadPoolExecutor@22927a81[Running, pool size = 2, active threads = 2, queued tasks = 2, completed tasks = 0]
demo-pool-0: objects 1:10001966
demo-pool-0:logMetric2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC(Microsoft Foundation Class)是一套用于开发Windows桌面应用程序的C++类库。MFC提供了许多功能和工具,包括线程池,用于处理并发执行的任务。下面是一个使用MFC线程池的详细示例: 1. 首先,在你的MFC应用程序中,包含头文件 afxmt.h,该文件定义了MFC中线程池相关的类和函数。 2. 创建一个继承自CWinApp的应用程序类,并在构造函数中调用SetAppCompatiblityMode函数,以确保应用程序与线程池兼容。示例代码如下: ```cpp class CMyApp : public CWinApp { public: CMyApp() { SetAppCompatiblityMode(_WIN32_WINNT_WIN7); } // ... }; ``` 3. 在你的主窗口类中,添加一个成员变量用于表示线程池,并在窗口的创建过程中初始化该线程池。示例代码如下: ```cpp class CMyWnd : public CFrameWnd { private: CThreadPool m_threadPool; public: CMyWnd() { m_threadPool.Initialize(); } // ... }; ``` 4. 在需要使用线程池的地方,创建一个继承自CWorkerThread的工作线程类,并重写其Run方法,该方法定义了线程需要执行的任务。示例代码如下: ```cpp class CMyWorkerThread : public CWorkerThread { public: virtual DWORD Run() { // 执行任务的代码 // ... return 0; } }; ``` 5. 在主窗口类中,创建并启动工作线程。示例代码如下: ```cpp void CMyWnd::StartWorkerThread() { CMyWorkerThread* pWorkerThread = new CMyWorkerThread(); m_threadPool.AddWorkerThread(pWorkerThread); pWorkerThread->Run(); } ``` 通过以上步骤,你就可以在MFC应用程序中使用线程池来处理并发任务了。线程池会自动管理线程的创建、销毁和调度,你只需要定义好工作线程类的任务代码即可。 注意:以上示例代码仅为演示用途,实际使用中需要根据具体需求进行适当的修改和扩展。另外,MFC线程池的使用还涉及更多细节,如任务队列、线程同步等,你可以参考MFC文档和相关教程来深入学习和理解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值