Parallel in Java Thread


Create package demo and create new java class as below:

package demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ParallelTasks {

	private final Collection<Runnable> tasks = new ArrayList<Runnable>();

	public void add(final Runnable task) {
		tasks.add(task);
	}

	public void go() throws InterruptedException {
		final ExecutorService threads = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
		try {
			final CountDownLatch latch = new CountDownLatch(tasks.size());
			for (final Runnable task : tasks)
				threads.execute(new Runnable() {
					public void run() {
						try {
							task.run();
						} finally {
							latch.countDown();
						}
					}
				});
			latch.await();
		} finally {
			threads.shutdown();
		}
	}

}




In demo package, create Main java class as below:

package demo;

import java.util.ArrayList;
import java.util.List;

public class Main {

	public static void main(String[] args) {

		try {
			List<Integer> a = new ArrayList<Integer>();
			a.add(5);
			a.add(-2);
			a.add(11);
			a.add(20);
			a.add(-3);
			a.add(9);
			a.add(-4);
			ParallelTasks tasks = new ParallelTasks();
			final Runnable countPositiveNumbers = new Runnable() {
				public void run() {
					try {
						int count = 0;
						for (int i = 0; i < a.size(); i++) {
							if (a.get(i) > 0) {
								count++;
							}
						}
						System.out.println("Count Positive Numbers: " + count);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						System.err.println(e.getMessage());
					}
				}
			};
			final Runnable countNegativeNumbers = new Runnable() {
				public void run() {
					try {
						int count = 0;
						for (int i = 0; i < a.size(); i++) {
							if (a.get(i) < 0) {
								count++;
							}
						}
						System.out.println("Count Negative Numbers: " + count);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						System.err.println(e.getMessage());
					}
				}
			};
			final Runnable countEvenNumbers = new Runnable() {
				public void run() {
					try {
						int count = 0;
						for (int i = 0; i < a.size(); i++) {
							if (a.get(i) % 2 == 0) {
								count++;
							}
						}
						System.out.println("Count Even Numbers: " + count);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						System.err.println(e.getMessage());
					}
				}
			};
			final Runnable countOddNumbers = new Runnable() {
				public void run() {
					try {
						int count = 0;
						for (int i = 0; i < a.size(); i++) {
							if (a.get(i) % 2 != 0) {
								count++;
							}
						}
						System.out.println("Count Odd Numbers: " + count);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						System.err.println(e.getMessage());
					}
				}
			};
			tasks.add(countEvenNumbers);
			tasks.add(countOddNumbers);
			tasks.add(countPositiveNumbers);
			tasks.add(countNegativeNumbers);
			tasks.go();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

	}

}




Count Odd Numbers: 4
Count Positive Numbers: 4
Count Even Numbers: 3
Count Negative Numbers: 3