code for thread creation (#12971)

* code for thread creation

* code refactored

* added code for executor service thread pools

* moved the files to a single module

Co-authored-by: Vartika_Nigam <Vartika_Nigam@DellTeam.com>
This commit is contained in:
Vartika Nigam 2022-11-16 08:39:18 +05:30 committed by GitHub
parent eb131e7f94
commit eae4d33d9c
6 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.concurrent.threads.create;
public class CustomThread extends Thread{
@Override
public void run(){
System.out.println(Thread.currentThread().getName()+" started");
}
public static void main(String[] args){
CustomThread t1 = new CustomThread();
t1.start();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.concurrent.threads.create;
public class TestClass implements Runnable{
@Override
public void run(){
System.out.println(Thread.currentThread().getName()+" started");
}
public static void main(String[] args){
TestClass testClassRef = new TestClass();
Thread t1 = new Thread(testClassRef);
t1.start();
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.concurrent.threads.create.threadpools;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CachedThreadPool {
public static void main(String[] args){
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(()->{
System.out.println("submitted a runnable task to cached thread pool");
System.out.println(Thread.currentThread().getName()+" from pool is executing the task");
});
executorService.shutdown();
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.concurrent.threads.create.threadpools;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FixedThreadPool{
public static void main(String[] args){
ExecutorService executorService = Executors.newFixedThreadPool(5);
executorService.submit(()->{
System.out.println("submitted a runnable task to fixed thread pool");
System.out.println(Thread.currentThread().getName()+" from pool is executing the task");
});
executorService.shutdown();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.concurrent.threads.create.threadpools;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPool{
public static void main(String[] args){
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
executorService.schedule(()->System.out.println("executing scheduled runnable task"), 5, TimeUnit.SECONDS);
executorService.shutdown();
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.concurrent.threads.create.threadpools;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class SingleThreadPool{
public static void main(String[] args){
ExecutorService executorService = Executors.newSingleThreadExecutor();
try{
Future<String> result = executorService.submit(()-> "callable task executed");
System.out.println(result.get());
}catch(InterruptedException | ExecutionException e){
e.printStackTrace();
}
executorService.shutdown();
}
}