Threads?
In extends Thread --run() method is optional
Ex:
public class MyThread extends Thread{
/*public void run(){
System.out.println(Thread.currentThread().getName());
}*/
}
class swapshari{
public static void main(String args[]){
MyThread mt=new MyThread();
mt.setName("one");
mt.start();
}
}
Implements Runnable –run() method has to override in implemented class.
Ex: public class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
class swapshari{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.setName("one");
t.start();
}
}
Thread States:
DNE-don’t exists (new)
Runnable
Running
Dead
(Blocked)
Methods from java.lang.Thread class
Sleep ()-this is the minimum time that the thread will be in block state. After sleep time it is not sure that the thread will go to running state, it will go to runnable state.
Sleep may throw InterruptedException.
try{
//do what you want to do before sleeping
Thread.currentThread().sleep(1000);//sleep for 1000 ms
//do what you want to do after sleeping
}
catch(ItrerruptedException ie){
//If this thread was interrupted by another thread
}
Yield() - Sent current executing thread to runnable state from running state, with same priority.
Join()-Non-static join() method of class Thread lets one thread join onto the end of another thread.
If you have a thread B that can't do its work until another thread A has completed its work, then you want thread B to "join" thread A
Methods from java.lang.Object class
Wait()
Notify()
notifyAll()
The Java language includes three important methods that effectively allow one thread to signal to another
Put simply, this is how signalling between threads works using 'wait and notify':
- We can call the wait() method of any Java object, which suspends the current thread. The thread is said to be "waiting on" the given object.
- Another thread calls the notify() method of the same Java object. This "wakes up" one of the threads waiting on that object.
When to use wait()/notify()
- For controlling shared resources ("pooling") such as database connections. If all resources are currently in use, one thread can wait to be notified that a resource has become available.
- For background execution or coordinating multi-threaded execution: the controlling thread can wait for other threads to notify it of completion of a task.
- For creating thread pools or job queues on a server: a fixed number of threads would sit waiting to be notified that a new job had been added to the list.
These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized method
- wait( ) tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
- notify( ) wakes up the first thread that called wait( ) on the same object.
- notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.
These methods are declared within Object, as shown here:
final void wait( ) throws InterruptedException final void notify( ) final void notifyAll( )
Ex:
synchronized int get() { if(!valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Got: " + n); valueSet = false; notify(); return n; }
Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.
setDaemon (true/false) – This method is used to set the thread to daemon thread. public boolean isDaemon() – This method is used to determine the thread is daemon thread or not
public class MyThread extends Thread{
/*public void run(){
System.out.println(Thread.currentThread().getName());
}*/
}
class swapshari{
public static void main(String args[]){
MyThread mt=new MyThread();
mt.setName("one");
mt.start();
}
}
Ex: public class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
class swapshari{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.setName("one");
t.start();
}
}
Thread States:
DNE-don’t exists (new)
Runnable
Running
Dead
(Blocked)
Methods from java.lang.Thread class
Sleep ()-this is the minimum time that the thread will be in block state. After sleep time it is not sure that the thread will go to running state, it will go to runnable state.
Sleep may throw InterruptedException.
try{
//do what you want to do before sleeping
Thread.currentThread().sleep(1000);//sleep for 1000 ms
//do what you want to do after sleeping
}
catch(ItrerruptedException ie){
//If this thread was interrupted by another thread
}
Yield() - Sent current executing thread to runnable state from running state, with same priority.
Join()-Non-static join() method of class Thread lets one thread join onto the end of another thread.
If you have a thread B that can't do its work until another thread A has completed its work, then you want thread B to "join" thread A
Methods from java.lang.Object class
Wait()
Notify()
notifyAll()
The Java language includes three important methods that effectively allow one thread to signal to another
When to use wait()/notify()
These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized method
Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.
setDaemon (true/false) – This method is used to set the thread to daemon thread. public boolean isDaemon() – This method is used to determine the thread is daemon thread or not
No comments:
Post a Comment