28/02/25 ----------- Q)Modify the above application(MThreading.java) so as to have only one user defined thread class. //MThreading2.java class ThreadOne extends Thread{ public void run(){ Thread t=Thread.currentThread();//returns the currently running thread reference String name=t.getName(); if(name.equals("one")){ for(int i=1;i<=20;i++){ System.out.println("Thread 1:"+i); }//task1 } else{ for(int i=21;i<=40;i++){ System.out.println("Thread 2:"+i); }//task2 } }//body of the thread } class MThreading2{ public static void main(String[] args){ ThreadOne t1=new ThreadOne(); ThreadOne t2=new ThreadOne(); t1.setName("one"); t2.setName("two"); t1.start(); t2.start(); } } Q)Modify the first multithreaded application(MThreading.java) so as to use Runnable interface. =>When a user defined Java class is inheriting from another Java class and for this user defined class if we want to apply multithreading, that class can't extend java.lang.Thread class. In this scenario, java.lang.Runnable interface is used to implement multithreading. Note:- Whenever user defined class is implementing Runnable interface, we have to create the thread objects by instantiating java.lang.Thread class. To the Thread class constructor we have to supply Runnable object as argument. Note:- Runnable interface object means, object of the user defined class that implements Runnable interface. //MThreading3.java class ThreadOne implements Runnable{ public void run(){ for(int i=1;i<=20;i++){ System.out.println("Thread 1:"+i); } } } class ThreadTwo implements Runnable{ public void run(){ for(int i=21;i<=40;i++){ System.out.println("Thread 2:"+i); } } } class MThreading3{ public static void main(String[] args) { Thread t1=new Thread(new ThreadOne()); Thread t2=new Thread(new ThreadTwo()); t1.start(); t2.start(); } }