Opening Hours :7AM to 9PM
Write a java program to display message a 2sec time gap.
class Demo extends Thread
{
public void run()
{
for(int i=0;i<=10;i++)
{
System.out.println("hello sateesh");
try
{
Thread.sleep(2000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
}
class Main
{
public static void main(String args[])
{
Demo d=new Demo();
d.start();
}
}
class Demo implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("hello sateesh");
try
{
Thread.sleep(3000);
}
catch(InterruptedException id)
{
System.out.println(id);
}
}
}
}
class Main
{
public static void main(String args[])
{
Demo d=new Demo();
Thread t=new Thread(d);
t.start();
}
}
class Cal extends Thread
{
int x,y,z1,z2;
void add()
{
x=20;
y=30;
z1=x+y;
}
void sub()
{
x=30;
y=40;
z2=x-y;
}
public void run()
{
add();
System.out.println("z1="+z1);
try
{
Thread.sleep(3000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
sub();
System.out.println("z2="+z2);
}
}
class Main
{
public static void main(String args[])
{
Cal c1=new Cal();
c1.start();
Cal c2=new Cal();
c2.start();
Cal c3=new Cal();
c3.start();
}
}
setName():
which can be used to set a user defined name for any thread.
UserdefinedThread objerf=new UserdefinedThread();
Objerf.setName("MyThread");
getName():
which can be used to get the current thread name
System.out.prinln(obj.getName());
NOTE:
If no user defined name was given by default thread name will starts from thread-0, thread-1,………
setPriority():
which can be used to set priority for a thread ,thread priority between 1 to 10.
1 Minimum priority
-------
-------
5 Normal priority
-------
-------
10 Maximum priority
The above three properties are existing on a predefined constant data members instead class shown below,
public static final int MIN_PRIORITY=1;
public static final int NORM_PRIORITY=5;
public static final int MAX_PRIORITY=10;
Syntax:
Obj.setPriority(any value between 1 to 10)
Obj.setPriority(Thread.MAX_PRIORITY)
getPriority():
which can be used to get the current priority of thread.
Syntax:
System.out.println(obj.getPriority());
NOTE:
By default priority for any thread is 5 (normal priority).
Sleep():
Which can be used to convert running state thread to waiting state .after specific time automatically thread with change from waiting to running state.
Syntax:
public static final void sleep(milliseconds) throws InterruptedException
{
try
{
Thread .sleep(any integer value)
}
catch(InterruptedException ie)
{
--
}
}
Suspend():
which is also can be used to change the running state into waiting state thread.
Obj.suspend();
If multiple threads are trying the same resource (working on same object) then there may be a chance of getting irreparable results ,to overcome this problem thread synchronization was introduced.
Definition:
Restricting of a thread to with a resource if already that resource is utilize by another thread is known as thread synchronization or thread safe.
In java language thread synchronization can be achieved in two different ways
1.Using synchronized block.
2.Using synchronized method.
Without synchronized
class Reserve implements Runnable
{
int available=1;
int wanted;
Reserve (int i)
{
wanted=1;
}
public void run()
{
System.out.println("avalable berts"+available);
if(available>=wanted)
{
String name=Thread.currentThread().getName();
System.out.println(wanted+"berts reserved for person"+name);
try
{
Thread.sleep(2000);
available=available-wanted;
}
catch(Exception e)
{
System.out.println(e);
}
}
else
{
System.out.println("sorry ! no berths");
}
}
}
class Main
{
public static void main(String args[])
{
Reserve r=new Reserve(1);
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.start();
t2.start();
}
}
avalable berts 1
1 berts reserved for person Thread-0
avalable berts 1
1 berts reserved for person Thread-1
class Reserve implements Runnable
{
int available=1;
int wanted;
Reserve (int i)
{
wanted=1;
}
public void run()
{
synchronized(this)
{
System.out.println("avalable berts"+available);
if(available>=wanted)
{
String name=Thread.currentThread().getName();
System.out.println(wanted+"berts reserved for person"+name);
try
{
Thread.sleep(2000);
available=available-wanted;
}
catch(Exception e)
{
System.out.println(e);
}
}
else
{
System.out.println("sorry ! no berths");
}
}
}
}
class Main
{
public static void main(String args[])
{
Reserve r=new Reserve(1);
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.start();
t2.start();
}
}
class Reserve implements Runnable
{
int available=1;
int wanted;
Reserve (int i)
{
wanted=1;
}
synchronized public void run()
{
System.out.println("avalable berts"+available);
if(available>=wanted)
{
String name=Thread.currentThread().getName();
System.out.println(wanted+"berts reserved for person"+name);
try
{
Thread.sleep(2000);
available=available-wanted;
}
catch(Exception e)
{
System.out.println(e);
}
}
else
{
System.out.println("sorry ! no berths");
}
}
}
class Main
{
public static void main(String args[])
{
Reserve r=new Reserve(1);
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.start();
t2.start();
}
}