Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

executors java

package pg01.c04;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
	public static void main(String[] args) {
		//ExecutorService executor = Executors.newFixedThreadPool(1); //Creates 1 thread in the pool for executting all
		//ExecutorService executor = Executors.newFixedThreadPool(2); //Creates 2 threads in the pool for executting all
		//ExecutorService executor = Executors.newFixedThreadPool(3); //Creates 3 threads in the pool for executting all
		//ExecutorService executor = Executors.newSingleThreadExecutor(); //Cretaes 1 single thread for the execution
		ExecutorService executor = Executors.newCachedThreadPool(); //Cretaes as many threads as it needs and reuses the ones created 
		//creates the logic threads but it doesn´t mean that its going to use 3 threads in the execution
		Thread hilox = new Thread(new MyThread("X",100, 100));
		Thread hiloO = new Thread(new MyThread("O",100, 100));
		Thread hilo_ = new Thread(new MyThread("_",100, 100));
		//Executes the threads
		executor.execute(hilo_); 
		executor.execute(hiloO);
		executor.execute(hilox);
		// closes the threads because if not they will remain waiting although the execution finished
		executor.shutdown();
		
		

	}
}
public class MyThread implements Runnable{
	private String s;
	private int l;
	private int r;
	public MyThread (String s, int l,int r) {
		this.s= s;
		this.l= l;
		this.r= r;
	}
	public void run() {
		// TODO Auto-generated method stub
		for(int i =0; i<r;i++) {
			System.out.print(s);
			for (int j=0;j<10000;j++) {
				System.out.print("*");
			}
			try {
				Thread.sleep(l);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println();
	}
}
Comment

executors java

ExecutorService executor = Executors.newFixedThreadPool(10);
Comment

PREVIOUS NEXT
Code Example
Java :: java classes and objects 
Java :: how to create a 2d arraylist java 
Java :: Duplicate class com.google.android.gms.internal.firebase_messaging.zzo found in modules jetified-firebase-iid 
Java :: java 8 seconds to days 
Java :: math.round java 
Java :: date to timestamp java 
Java :: java to kotlin tutorial 
Java :: java anonymous function 
Java :: binary search program in java 
Java :: how to access methods from another class in java 
Java :: find number of weeks between two dates in java 
Java :: extract html tag using regex 
Java :: java string start with substring 
Java :: log4j2.properties file need to save in spring boot application 
Java :: error: incompatible types: NonExistentClass cannot be converted to Annotation 
Java :: connection data base java code 
Java :: java random value threadlocalrandom 
Java :: iptc classification java code example 
Java :: jbutton 
Sql :: delete mysql ubuntu 20.04 
Sql :: mysql list bigger table 
Sql :: forgot my mysql password mac 
Sql :: oracle create synonym 
Sql :: postgresql generate uuid 
Sql :: postgres kill running query 
Sql :: postgres alter user password 
Sql :: mysql update part of string 
Sql :: sql server connection string in .net core with password 
Sql :: select records for day before yesterday mysql 
Sql :: oracle sql log to console 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =