In this session we will learn some multithread programs which are very useful in written test as well as interview. A multithread program contains two or more parts that can run concurrently. This means that single program can perform more tasks at the same time.The best example is video game.
1) How to get the name of the running thread?
class Test extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
display();
}
}
public void display()
{
Thread t=Thread.currentThread();
String name=t.getName();
System.out.println("name="+name);
}
public static void main(String args[])
{
Test t1=new Test();
t1.start();
for(int i=0;i<10;i++)
{
t1.display();
}
}
}
Output:
2) How to set priority of a Thread?
class MyThread extends Thread
{
public void run()
{
for (int i=0; i< 10 ; i++ )
{
System.out.println("Child Thread");
}
}
}
class ThreadPriorityDemo
{
public static void main(String arg[])
{
MyThread t = new MyThread();
System.out.println(t.getPriority());
t.setPriority(10);
t.start();
for(int i =0;i<10;i++)
{
System.out.println("Main Thread");
}
}
}
Output:
3) How to prevent a thread from execution using yield() method
class MyThread extends Thread
{
public void run()
{
for (int i=0; i< 10 ; i++ )
{
System.out.println("Child Thread");
Thread.yield();
}
}
}
class YieldDemo
{
public static void main(String arg[])
{
MyThread t = new MyThread();
t.start();
for(int i =0;i<10;i++)
{
System.out.println("Main Thread");
}
}
}
Output:
4) How to prevent a Thread from execution using sleep()
class MyThread extends Thread
{
public void run()
{
try
{
for (int i = 0;i<10;i++)
{
System.out.println("This is Lazy Method");
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class SleepDemo
{
public static void main(String arg[])throws InterruptedException
{
MyThread t = new MyThread();
t.start();
System.out.println("Main Thread");
}
}
Output:
Read for more details: Multithreading in java
5) How can you interrupt a sleep() method of thread class
class MyThread extends Thread
{
public void run()
{
try
{
for (int i = 0;i<10;i++)
{
System.out.println("This is Lazy Method");
Thread.sleep(3000);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class InterruptDemo
{
public static void main(String arg[])throws InterruptedException
{
MyThread t = new MyThread();
t.start();
t.interrupt();
System.out.println("Main Thread");
}
}
Output:
6) Every object in has unique lock but a thread can acquire more than one lock at a time. Explain it?
class Printmsg
{
public synchronized void wish(String name)
{
for(int i =0;i<10;i++)
{
System.out.print("Hi....");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
System.out.println(name);
}
}
}
class MyThread extends Thread
{
Printmsg p;
String name;
MyThread(Printmsg p,String name)
{
this.p = p;
this.name = name;
}
public void run()
{
p.wish(name);
}
}
class SynchronizedTest
{
public static void main(String arg[])
{
Printmsg p = new Printmsg();
MyThread t1 = new MyThread(p,"lucky");
MyThread t2 = new MyThread(p,"good morning");
t1.start();
t2.start();
}
}
Output:
7) Is daemon() thread will start early or main()method?
we can not change the daemon() nature of main()thread because it has started already before main() method only. All daemon() threads terminated automatically when ever lost non-daemon threads.
class MyThread extends Thread
{
public void run()
{
for(int i = 0;i<10;i++)
{
System.out.println("Child Thread");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
}
}
class DaemonThreadDemo
{
public static void main(String arg[])
{
MyThread t = new MyThread();
t.setDaemon(true);
t.start();
System.out.println("The end of main");
}
}
Output:
If you comment setDaemon(true) then both child and main threads are non-daemon,if we are not comment then child thread is daemon thread and hence it will terminate automatically when ever main() thread terminates.
Note: Deamon thread means the threads which are running in the background to provide support for user defined threads are called Deamon threads
8) How to display thread status?
class myThread extends Thread
{
boolean waiting=true;
boolean ready=false;
myThread()
{
}
public void run()
{
String threadName=Thread.currentThread().getName();
System.out.println(threadName+"starting");
while(waiting)
System.out.println("waiting:"+waiting);
startWait();
try{
Thread.sleep(2000);
}
catch(Exception e)
{
System.out.println(threadName+"inerrupted");
}
System.out.println(threadName+"terminating");
}
synchronized void startWait()
{
try
{
while(!ready)
wait();
}
catch(InterruptedException ie)
{
System.out.println("wait() interrupted()");
}
}
synchronized void notice()
{
ready=true;
notify();
}
}
public class Demo
{
public static void main(String args[])throws Exception
{
myThread mt=new myThread();
mt.setName("My Thread 1");
showThreadStatus(mt);
mt.start();
Thread.sleep(100);
showThreadStatus(mt);
mt.waiting=false;
Thread.sleep(100);
showThreadStatus(mt);
mt.notice();
Thread.sleep(100);
showThreadStatus(mt);
while(mt.isAlive())
System.out.println("alive");
showThreadStatus(mt);
}
static void showThreadStatus(Thread mt)
{
System.out.println(mt.getName()+"alive:"+mt.isAlive()+"state"+mt.getState());
}
}
Output:
Read also for Multithread interview Q&A : Java multithreading interview Questions and answers
1) How to get the name of the running thread?
class Test extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
display();
}
}
public void display()
{
Thread t=Thread.currentThread();
String name=t.getName();
System.out.println("name="+name);
}
public static void main(String args[])
{
Test t1=new Test();
t1.start();
for(int i=0;i<10;i++)
{
t1.display();
}
}
}
Output:
2) How to set priority of a Thread?
class MyThread extends Thread
{
public void run()
{
for (int i=0; i< 10 ; i++ )
{
System.out.println("Child Thread");
}
}
}
class ThreadPriorityDemo
{
public static void main(String arg[])
{
MyThread t = new MyThread();
System.out.println(t.getPriority());
t.setPriority(10);
t.start();
for(int i =0;i<10;i++)
{
System.out.println("Main Thread");
}
}
}
Output:
3) How to prevent a thread from execution using yield() method
class MyThread extends Thread
{
public void run()
{
for (int i=0; i< 10 ; i++ )
{
System.out.println("Child Thread");
Thread.yield();
}
}
}
class YieldDemo
{
public static void main(String arg[])
{
MyThread t = new MyThread();
t.start();
for(int i =0;i<10;i++)
{
System.out.println("Main Thread");
}
}
}
Output:
4) How to prevent a Thread from execution using sleep()
class MyThread extends Thread
{
public void run()
{
try
{
for (int i = 0;i<10;i++)
{
System.out.println("This is Lazy Method");
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class SleepDemo
{
public static void main(String arg[])throws InterruptedException
{
MyThread t = new MyThread();
t.start();
System.out.println("Main Thread");
}
}
Output:
Read for more details: Multithreading in java
5) How can you interrupt a sleep() method of thread class
class MyThread extends Thread
{
public void run()
{
try
{
for (int i = 0;i<10;i++)
{
System.out.println("This is Lazy Method");
Thread.sleep(3000);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class InterruptDemo
{
public static void main(String arg[])throws InterruptedException
{
MyThread t = new MyThread();
t.start();
t.interrupt();
System.out.println("Main Thread");
}
}
Output:
6) Every object in has unique lock but a thread can acquire more than one lock at a time. Explain it?
class Printmsg
{
public synchronized void wish(String name)
{
for(int i =0;i<10;i++)
{
System.out.print("Hi....");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
System.out.println(name);
}
}
}
class MyThread extends Thread
{
Printmsg p;
String name;
MyThread(Printmsg p,String name)
{
this.p = p;
this.name = name;
}
public void run()
{
p.wish(name);
}
}
class SynchronizedTest
{
public static void main(String arg[])
{
Printmsg p = new Printmsg();
MyThread t1 = new MyThread(p,"lucky");
MyThread t2 = new MyThread(p,"good morning");
t1.start();
t2.start();
}
}
Output:
7) Is daemon() thread will start early or main()method?
we can not change the daemon() nature of main()thread because it has started already before main() method only. All daemon() threads terminated automatically when ever lost non-daemon threads.
class MyThread extends Thread
{
public void run()
{
for(int i = 0;i<10;i++)
{
System.out.println("Child Thread");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
}
}
class DaemonThreadDemo
{
public static void main(String arg[])
{
MyThread t = new MyThread();
t.setDaemon(true);
t.start();
System.out.println("The end of main");
}
}
Output:
If you comment setDaemon(true) then both child and main threads are non-daemon,if we are not comment then child thread is daemon thread and hence it will terminate automatically when ever main() thread terminates.
Note: Deamon thread means the threads which are running in the background to provide support for user defined threads are called Deamon threads
8) How to display thread status?
class myThread extends Thread
{
boolean waiting=true;
boolean ready=false;
myThread()
{
}
public void run()
{
String threadName=Thread.currentThread().getName();
System.out.println(threadName+"starting");
while(waiting)
System.out.println("waiting:"+waiting);
startWait();
try{
Thread.sleep(2000);
}
catch(Exception e)
{
System.out.println(threadName+"inerrupted");
}
System.out.println(threadName+"terminating");
}
synchronized void startWait()
{
try
{
while(!ready)
wait();
}
catch(InterruptedException ie)
{
System.out.println("wait() interrupted()");
}
}
synchronized void notice()
{
ready=true;
notify();
}
}
public class Demo
{
public static void main(String args[])throws Exception
{
myThread mt=new myThread();
mt.setName("My Thread 1");
showThreadStatus(mt);
mt.start();
Thread.sleep(100);
showThreadStatus(mt);
mt.waiting=false;
Thread.sleep(100);
showThreadStatus(mt);
mt.notice();
Thread.sleep(100);
showThreadStatus(mt);
while(mt.isAlive())
System.out.println("alive");
showThreadStatus(mt);
}
static void showThreadStatus(Thread mt)
{
System.out.println(mt.getName()+"alive:"+mt.isAlive()+"state"+mt.getState());
}
}
Output:
Read also for Multithread interview Q&A : Java multithreading interview Questions and answers
No comments:
Post a Comment