Question: Java: writing code on eclipse ide 1. Create a new class that constructs an instance of MultiTheadingDemo3. Choose your own loop count. 2. Create a

Java: writing code on eclipse ide

1. Create a new class that constructs an instance of MultiTheadingDemo3. Choose your own loop count.

2. Create a new class that constructs an instance of MultiTheadingDemo4. Choose your own loop count.

3. Do a bit of research on the use of Threads in Java. Describe an instance when the application of Threads would be useful.

Below is the full code:

package threads;

//Main Class

public class Multithread

{

public static void main(String[] args)

{

int n = 8; // Number of threads

for (int i=0; i

{

MultiThreadingDemo3 object = new MultiThreadingDemo3();

object.start();

}

}

}

package threads;

public class Multithread2 {

public static void main(String[] args)

{

int n = 8; // Number of threads

for (int i=0; i

{

Thread object = new Thread(new MultiThreadingDemo4());

object.start();

}

}

}

package threads;

public class MultiThreadingDemo extends Thread{

public void run(){

System.out.println("Demo - My thread is in running state.");

}

public static void main(String args[]){

MultiThreadingDemo obj=new MultiThreadingDemo();

obj.start();

}

}

package threads;

public class MultiThreadingDemo2 implements Runnable{

public void run(){

System.out.println("Demo 2 - My thread is in running state.");

}

public static void main(String args[]){

MultiThreadingDemo2 obj=new MultiThreadingDemo2();

Thread tobj =new Thread(obj);

tobj.start();

}

}

package threads;

//Java code for thread creation by extending

//the Thread class

public class MultiThreadingDemo3 extends Thread {

public void run()

{

try

{

// Displaying the thread that is running

System.out.println ("Demo 3 - Thread " +

Thread.currentThread().getId() +

" is running");

}

catch (Exception e)

{

// Throwing an exception

System.out.println ("Exception is caught");

}

}

}

package threads;

public class MultiThreadingDemo4 implements Runnable {

public void run()

{

try

{

// Displaying the thread that is running

System.out.println ("Demo 4 - Thread " +

Thread.currentThread().getId() +

" is running");

}

catch (Exception e)

{

// Throwing an exception

System.out.println ("Exception is caught");

}

}

}

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!