0% found this document useful (0 votes)
8 views

Java Assignment

The document discusses 5 examples of Java thread programming - extending Thread class, implementing Runnable interface, anonymous Runnable implementation, using ExecutorService for thread pool, and using Callable with Future for thread result.
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)
8 views

Java Assignment

The document discusses 5 examples of Java thread programming - extending Thread class, implementing Runnable interface, anonymous Runnable implementation, using ExecutorService for thread pool, and using Callable with Future for thread result.
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/ 4

JAVA ASSIGNMENT

Name: Y Sai Akhilesh


Roll no : CB.SC.U4AIE23348
Section: D
1. Extending Thread class:
java
public class ThreadExample extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Thread: " + i);
}
}

public static void main(String[] args) {


ThreadExample thread = new ThreadExample();
thread.start();
}
}

2. Implementing Runnable interface:


java
public class RunnableExample implements Runnable {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Runnable: " + i);
}
}

public static void main(String[] args) {


Thread = new Thread(new RunnableExample());
thread.start();
}
}

3. Anonymous Runnable implementation:


java
public class AnonymousRunnableExample {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Anonymous Runnable: " + i);
}
}
});
thread.start();
}
}

4. Using ExecutorService for thread pool:


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

public class ExecutorServiceExample {


public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 1; i <= 10; i++) {
final int taskNumber = i;
executor.submit(() -> System.out.println("Task: " + taskNumber));
}
executor.shutdown();
}
}

5. Using Callable with Future for thread result:


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

public class CallableFutureExample {


public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> futureResult = executor.submit(new
FactorialCalculator(5));
try {
int result = futureResult.get();
System.out.println("Factorial result: " + result);
} catch (Exception e) {
e.printStackTrace();
}

executor.shutdown();
}

static class FactorialCalculator implements Callable<Integer> {


private final int number;

public FactorialCalculator(int number) {


this.number = number;
}

public Integer call() {


int factorial = 1;
for (int i = 2; i <= number; i++) {
factorial *= i;
}
return factorial;
}
}
}

You might also like