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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started