Question: Exercise 1 : public class TwoThread extends Thread { public void run ( ) { for ( int i = 0 ; i < 2

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
2. Compile and execute the program
Note: starting a thread is done via the start () method. The main () method is itself in a thread (the main thread)
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 ?
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).
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 e

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!