zoukankan      html  css  js  c++  java
  • 用线程池开启两个线程遍历

    package com.psd.test;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    import org.junit.Test;
    
    public class ThreadDemo {
    	// 使用线程池,长度为2
    	private final static ExecutorService pool = Executors.newFixedThreadPool(2);
    	final long waitTime = 8 * 1000;
    	final static long awaitTime = 5 * 1000;
    
    	// 对于100个数,开启你两个线程遍历
    
    	// 定义一个内部线程类
    	class CountThread implements Runnable {
    		private long a;
    
    		public CountThread(long a) {
    			super();
    			this.a = a;
    		}
    
    		@Override
    		public void run() {
    			long count = 0;
    			for (int i = 0; i < a; i++) {
    				count = count + i;
    				for (int j = 0; j < a; j++) {
    					count = count + j;
    
    				}
    			}
    			System.err.println(count + Thread.currentThread().getName() + new Date());
    		}
    
    	}
    
    	@Test
    	public void test02() {
    		// 比如,要去干封装好的同样的事情
    		// 比如有100个数打印
    		// 如何让两个线程干这件事呢
    		List<Runnable> runnables = new ArrayList<Runnable>();
    		CountThread runnable3 = new CountThread(100000);
    		CountThread runnable1 = new CountThread(100000);
    		CountThread runnable2 = new CountThread(100000);
    		runnables.add(runnable3);
    		runnables.add(runnable1);
    		runnables.add(runnable2);
    		for (Runnable runnable : runnables) {
    			pool.execute(runnable);
    		}
    		pool.shutdown();
    	}
    
    	// 方法测试
    	public static void main(String[] args) {
    		System.err.println(Thread.currentThread().getName() + new Date());
    		ThreadDemo threadDemo = new ThreadDemo();
    		threadDemo.test02();
    	}
    
    	public void test03() {
    		this.test04(100000);
    		this.test04(100000);
    		this.test04(100000);
    
    	}
    
    	public void test04(long a) {
    		long count = 0;
    		for (int i = 0; i < a; i++) {
    			count = count + i;
    			for (int j = 0; j < a; j++) {
    				count = count + j;
    
    			}
    		}
    		System.err.println(count);
    	}
    
    }
    

      

  • 相关阅读:
    1260. [CQOI2007]涂色【区间DP】
    2733. [HNOI2012]永无乡【平衡树-splay】
    1087. [SCOI2005]互不侵犯King【状压DP】
    1026. [SCOI2009]windy数【数位DP】
    1066. [SCOI2007]蜥蜴【最大流】
    luogu P2776 [SDOI2007]小组队列
    cogs 717. [SDOI2007] 小组队列
    luogu P1160 队列安排
    2612. [FHZOI 2017]被窃的项链
    codevs 3336 电话网络 (2)
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/8681629.html
Copyright © 2011-2022 走看看