Java Multithread Programs

In previous post we were learned some of String programs,in this post you will learn multithread programs.

1. Java Program to find the name of the 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:

Javac Test.java
java Test
name=main
name=main
name=main
name=main
name=main
name=Thread-0
name=main
name=Thread-0
name=main
name=Thread-0
name=main
name=Thread-0
name=main
name=Thread-0
name=Thread-0
name=Thread-0

2) Java Program 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:
javac ThreadPriorityDemo.java
java ThreadPriorityDemo

5
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread

3) Java Program 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:
javac YiedldDemo.java
java YieldDemo
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
 
4) Java Program 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:
javac SleepDemo.java
java SleepDemo
Main Thread
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method

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:
javac InterruptDemo.java
java InterruptDemo

Main Thread
This is Lazy Method
java.lang.InterruptedException:sleep interrupted


6)  Every object 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:
 javac SynchronizedTest.java
java SynchronizedTest
Hi....good morning
Hi....good morning
Hi....good morning
Hi....good morning
Hi....good morning
Hi....good morning
Hi....good morning
Hi....good morning
Hi....good morning
Hi....good morning
Hi....lucky
Hi....lucky
Hi....lucky
Hi....lucky
Hi....lucky
Hi....lucky
Hi....lucky
Hi....lucky
Hi....lucky
Hi....lucky

 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:
javac DaemonThreadDemo.java
java DaemonThreadDemo
The end of main
child Thread

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:

Javac Demo.java
java Demo

alive
alive
alive
alive
alive
alive
alive
alive
alive
alive
alive
alive
My Thread 1terminating
alive
alive
alive
My Thread 1alive:falsestateTERMINATED

No comments:

Post a Comment

Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...