Merge remote-tracking branch 'origin/master'
This commit is contained in:
48
Java/ExecutorService/ExecutorServiceCallableExample.java
Normal file
48
Java/ExecutorService/ExecutorServiceCallableExample.java
Normal file
@@ -0,0 +1,48 @@
|
||||
import java.util.concurrent.*;
|
||||
|
||||
class CallableTask implements Callable<Integer>{
|
||||
private int id;
|
||||
|
||||
CallableTask(int id){
|
||||
this.id = id;
|
||||
}
|
||||
@Override
|
||||
public Integer call() {
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
public class ExecutorServiceCallableExample {
|
||||
|
||||
static void printFutureData(Future future){
|
||||
System.out.println("isDone : "+future.isDone());
|
||||
|
||||
try {
|
||||
System.out.println("get : "+future.get());
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ExecutorService service = Executors.newFixedThreadPool(2);
|
||||
|
||||
Future future = service.submit(new CallableTask(1));
|
||||
service.shutdown();
|
||||
|
||||
while(!future.isDone()){
|
||||
try {
|
||||
System.out.println("Waiting for the Future to complete ...");
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
printFutureData(future);
|
||||
}
|
||||
}
|
||||
31
Java/ExecutorService/ExecutorServiceExample.java
Normal file
31
Java/ExecutorService/ExecutorServiceExample.java
Normal file
@@ -0,0 +1,31 @@
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
class ExampleTask implements Runnable{
|
||||
private int id;
|
||||
|
||||
ExampleTask(int id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println("Started : "+id);
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Completed : "+id);
|
||||
}catch(InterruptedException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
public class ExecutorServiceExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ExecutorService service = Executors.newFixedThreadPool(2);
|
||||
for(int i = 0; i< 10; i++){
|
||||
service.execute(new ExampleTask(i));
|
||||
}
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
53
Java/ExecutorService/ExecutorServiceMultiThread.java
Normal file
53
Java/ExecutorService/ExecutorServiceMultiThread.java
Normal file
@@ -0,0 +1,53 @@
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
class TaskOne implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Running task 1");
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TaskTwo implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Running task 2");
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TaskThree implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Running task 3");
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ExecutorServiceMultiThread {
|
||||
public static void main(String[] args) {
|
||||
ExecutorService service = Executors.newFixedThreadPool(3);
|
||||
TaskOne firstTask = new TaskOne();
|
||||
TaskTwo secondTask = new TaskTwo();
|
||||
TaskThree thirdTask = new TaskThree();
|
||||
|
||||
service.execute(firstTask);
|
||||
service.execute(secondTask);
|
||||
service.execute(thirdTask);
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
22
Java/ExecutorService/ScheduledThreadPoolExecutorExample.java
Normal file
22
Java/ExecutorService/ScheduledThreadPoolExecutorExample.java
Normal file
@@ -0,0 +1,22 @@
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class RunnableTask implements Runnable{
|
||||
long startTime;
|
||||
|
||||
RunnableTask(){
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Seconds : "+((System.currentTimeMillis() - startTime)/1000));
|
||||
}
|
||||
}
|
||||
|
||||
public class ScheduledThreadPoolExecutorExample {
|
||||
public static void main(String[] args) {
|
||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
|
||||
executor.scheduleWithFixedDelay(new RunnableTask(), 3, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user