To turn on two thread traversal using a thread pool

Source: Internet
Author: User

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 {//Use thread pool, length 2private final static executorservice pool = Executors.newfixe  Dthreadpool (2); final long waitTime = 8 * 1000;final static Long awaittime = 5 * 1000;//for 100 numbers, turn on you two threads traverse//Define an internal threading class Countthread implements Runnable {private long a;public countthread (long a) {super (); this.a = A;} @Overridepublic 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 ());}} @Testpublic void test02 () {//For example, to do the same thing in a packaged package//For example, there are 100 numbers to print//How to get two threads to do this thing list<runnable> runnables = new arraylist& Lt 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 ();} The method tests 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);}}

  

To turn on two thread traversal using a thread pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.