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

Problem 01: Thread: Create Thread: by Extending Thread Class With Sleep Method

The document contains 6 problems related to creating and using threads in Java. Problem 1 creates a thread by extending Thread and using the sleep() method. Problem 2 creates a thread by extending Thread to compare with Problem 3. Problem 3 creates a thread by implementing Runnable instead of extending Thread. Problem 4 also implements Runnable but calls run() directly instead of using start() to compare with Problem 3. Problem 5 creates 3 threads and uses join() to wait for the first thread to finish before starting the others. Problem 6 is similar but uses a timeout with join().

Uploaded by

Jesmin Mostafa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Problem 01: Thread: Create Thread: by Extending Thread Class With Sleep Method

The document contains 6 problems related to creating and using threads in Java. Problem 1 creates a thread by extending Thread and using the sleep() method. Problem 2 creates a thread by extending Thread to compare with Problem 3. Problem 3 creates a thread by implementing Runnable instead of extending Thread. Problem 4 also implements Runnable but calls run() directly instead of using start() to compare with Problem 3. Problem 5 creates 3 threads and uses join() to wait for the first thread to finish before starting the others. Problem 6 is similar but uses a timeout with join().

Uploaded by

Jesmin Mostafa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

• Problem 01: Thread : Create thread by extending Thread class with sleep() method

package threadexamp;

class ex1 extends Thread{


@Override
public void run()
{
for(int i=0;i<5;i++)
{
try{
Thread.sleep(500);
}catch(Exception e){

}
System.out.println(i);
}

public class ThreadExamp {

public static void main(String[] args) {


ex1 t1=new ex1();
ex1 t2=new ex1();
t1.start();
t2.start();

}
• Problem 02: Thread : Create thread by extending Thread class for comparing with problem 3

class test1 extends Thread{

@Override
public void run()
{
System.out.println("Run method executed by child Thread");

public static void main(String[] args) {


test1 t1=new test1();
t1.start();

System.out.println("Main method executed by main thread");


}
}

Problem 03: Thread : Create thread by implementing Runnable interface


class Test implements Runnable {
@Override
public void run()
{
System.out.println("Run method executed by child Thread");
}
public static void main(String[] args)
{

Test t = new Test();

Thread t1 = new Thread(t);

t1.start();
System.out.println("Main method executed by main thread");
}
}
Problem 04: Thread : Create thread by implementing Runnable interface for comparing with Problem 03
where run() executing following thread rule(main() method will be executed first then others). But in
Problem 04 run() executing in normal sequence also run() executing following thread rule when passing
object through thread.
class Test implements Runnable {
@Override
public void run()
{
System.out.println("Run method executed by child Thread");
}
public static void main(String[] args)
{

Test t = new Test();


t.run();
Thread t1 = new Thread(t);
t1.start();
System.out.println("Main method executed by main thread");
}
}

Problem 05: Thread : Create thread and use join() method

class Joinmethod extends Thread{

public void run(){


for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
Joinmethod t1=new Joinmethod();
Joinmethod t2=new Joinmethod();
Joinmethod t3=new Joinmethod();
t1.start();
try{
t1.join();
}catch(Exception e){System.out.println(e);}

t2.start();
t3.start();
}
}
Problem 06: Thread : Create thread and use join() method

class Joinmethod extends Thread{

public void run(){


for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
Joinmethod t1=new Joinmethod();
Joinmethod t2=new Joinmethod();
Joinmethod t3=new Joinmethod();
t1.start();
try{
t1.join(1500);
}catch(Exception e){System.out.println(e);}

t2.start();
t3.start();
}
}

You might also like