All threads running example and jvisualvm analysis

public class AllThreadsRunningTest {

	public static void main(String[] args) throws InterruptedException {
		
		System.out.println(Thread.currentThread().getName() + " started");
		
		int availableProcessorsCount = Runtime.getRuntime().availableProcessors();
		System.out.println("availableProcessorsCount=" + availableProcessorsCount);
		
		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(availableProcessorsCount);
		
		for (int i = 0; i < availableProcessorsCount; i++) {
			fixedThreadPool.execute(() -> {
				System.out.println(Thread.currentThread().getName() + " started");
				int count = 0;
				while(true){
					count++;
				}
			});
		}
		
		System.out.println(Thread.currentThread().getName() + " scheduled threads");
	}
}

Output:
main started
availableProcessorsCount=4
pool-1-thread-1 started
pool-1-thread-2 started
main scheduled threads
pool-1-thread-3 started
pool-1-thread-4 started

AllThreadsRunning

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s