0% found this document useful (0 votes)
4 views5 pages

Printtask Merged

Ggh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Printtask Merged

Ggh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

package in.kgcoding.

executor;

public class PrintTask implements Runnable {


@Override
public void run() {
// Print task
for (int i = 1; i <= 100; i++) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.printf("%d%c ", i, targetChar);
}
System.out.printf("\n %s %c task complete",
Thread.currentThread().getName(),
targetChar);
}

private final char targetChar;

public PrintTask(char targetChar) {


this.targetChar = targetChar;
}
}
package in.kgcoding.executor;

import in.kgcoding.multithreading.runnable.PrintTask;

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

public class TestingSingleThreadExecutor {


public static void main(String[] args) {
ExecutorService service = Executors.newSingleThreadExecutor();
PrintTask task1 = new PrintTask('*');
PrintTask task2 = new PrintTask('$');
PrintTask task3 = new PrintTask('#');

service.submit(task1);
service.submit(task2);
service.submit(task3);

service.shutdown();
}
}
package in.kgcoding.executor;

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

public class TestingMultipleThreadExecutor {


public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);

for (int i = 0; i < 10; i++) {


PrintTask task = new PrintTask((char)i);
service.submit(task);
}

service.shutdown();
System.out.println("\n**************1");
if (!service.awaitTermination(10, TimeUnit.SECONDS)) {
System.out.println("\n**************2");
service.shutdownNow();
}

}
}
package in.kgcoding.executor.future;

import java.util.concurrent.Callable;

public class FetchName implements Callable<String> {

private final String name;

public FetchName(String name) {


this.name = name;
}

@Override
public String call() throws Exception {
System.out.printf("\n Getting full name of %s from server", name);
Thread.sleep(4000);
return name + " Bhartiya";
}
}
package in.kgcoding.executor.future;

import java.util.concurrent.*;

public class TestingFutures {


public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
FetchName task1 = new FetchName("Prashant");
FetchName task2 = new FetchName("Sanchit");
FetchName task3 = new FetchName("KG Coding");
FetchName task4 = new FetchName("Subscribe");

Future<String> name1 = service.submit(task1);


Future<String> name2 = service.submit(task2);
Future<String> name3 = service.submit(task3);
Future<String> name4 = service.submit(task4);

System.out.printf("\nFull Name is: %s", name1.get());


System.out.printf("\nFull Name is: %s", name2.get());
System.out.printf("\nFull Name is: %s", name3.get());
System.out.printf("\nFull Name is: %s", name4.get());

service.shutdown();
}
}

You might also like