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

Exercise 1

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)
19 views5 pages

Exercise 1

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

Exercise 1:

public class TwoThread extends Thread { public void run() { for ( int i = 0; i < 20; i++ ) {
System.out.println(Thread.currentThread ().getName()); } } public static void main(String[] args) {
TwoThread T1 = new TwoThread(); T1.start(); for ( int i = 0; i < 20; i++ ) {
System.out.println("Main thread"); } } }

1. What is it doing this program


Answer:
The program creates two threads: the main thread and an instance of the TwoThread class.
The run() method of the TwoThread class is overridden to print the name of the current
thread 20 times. In the main() method, the start() method is called on the TwoThread
instance to start its execution. Then, the main thread prints "Main thread" 20 times.

2. Compile and execute the program

Answer:

3-Modify the previous program so that: create 2 others threads T2 and T3 using the class Thread
and TwoThread. Execute the program and explain what you see ?

Answer:
public class TwoThread extends Thread {
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName());
}
}

public static void main(String[] args) {


TwoThread T1 = new TwoThread();
Thread T2 = new Thread(new TwoThread());
Thread T3 = new Thread(new TwoThread());

T1.start();
T2.start();
T3.start();

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


System.out.println("Main thread");
}
}
}

In this modified program, we create two additional threads (T2 and T3) using the Thread
class and passing an instance of the TwoThread class as the Runnable parameter. Then, we
start all three threads (T1, T2, and T3). When you execute this modified program, you will
see the output with interleaved thread names and "Main thread" messages from all three
threads. The exact interleaving of the output may vary on each run due to the concurrent
nature of the threads.

4-Modify the previous program so that the thread T2 complete its execution by blocking all
others thread after that they can run. Note: you can use join() method. The join() method is
used to hold the execution of currently running thread until the specified thread is
dead(finished execution).
Answer:
public class TwoThread extends Thread {
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName());
}
}

public static void main(String[] args) {


TwoThread T1 = new TwoThread();
Thread T2 = new Thread(new TwoThread());
Thread T3 = new Thread(new TwoThread());

T1.start();
try {
T2.start();
T2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
T3.start();

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


System.out.println("Main thread");
}
}
}
In this modified program, after starting T2 with T2.start(), we use T2.join() to block the e
xecution of the main thread until T2 completes its execution. This means that T2 will finish
running before T3 and the main thread are allowed to continue. As a result, you will see the
output from T1, then the output from T2, and finally the output from T3 and the main
thread, each printing their respective messages 20 times.

Exercise 2: Write in java a program that uses two threads in parallel: - the first will display
the 26 letters of the alphabet; - the second will display numbers from 1 to 26. - the second
thread must wait 200 ms after it end.
Answer:
public class AlphabetThread extends Thread {
public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c + " ");
}
}
}

public class NumberThread extends Thread {


public void run() {
for (int i = 1; i <= 26; i++) {
System.out.print(i + " ");
}
}
}
public class ParallelThreads {
public static void main(String[] args) {
AlphabetThread alphabetThread = new AlphabetThread();
NumberThread numberThread = new NumberThread();

alphabetThread.start();
try {
alphabetThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}

numberThread.start();
}
}

You might also like