Archive for January 2012
There are several methods used by the thread.
- Static methods
- Instance methods
- Static methods defined in the class are as follow:
- Thread currentThread()
- Returns a Thread object that encapsulates the thread that calls this method.
- Thread currentThread()
Or
- Returns a reference to the current thread.
- void sleep(long msec) throws InterruptedException
- Causes the current thread to wait for the msec millisecond.
Or
- Suspends execution of the thread for the specified number of milliseconds.
- void sleep(long msec, int nsec) throws InterruptedException
- Causes the current thread to wait for the msec millisecond plus nsec nanoseconds.
Or
- Suspends execution of the thread for the specified number of milliseconds plus specified nanoseconds.
- void yield()
- Causes the current thread to yield control of the processor (CPU) to another thread.
- Instance methods defined in the class are as follows:
- String getName()
- Returns the name of the thread.
- String getName()
- int getPriority()
- Returns the priority of the thread.
- boolean isAlive()
- Returns true if the specified thread has been started and has not yet died otherwise returns false.
- void join() throws InteruptedException
- causes the caller to wait until the specified thread dies.
- void join(long msec) throws InteruptedException
- Waits up to the specified number of milliseconds for the thread on which it is called to terminate.
- If the msec is zero (0) than there is no limit for the wait time.
- void join(long msec,int nsec) throws InteruptedException
- Waits up to the specified number of milliseconds plus nanoseconds for the thread on which it is called to terminate.
- If the msec plos nsec is zero (0) than there is no limit for the wait time.
- void run()
- Begins execution of thread.
Or
- Comprises the body of the thread, this method is overridden by subclass.
- void setPriority(int p)
- Sets the priority of this thread to p.
- void setName(String s)
- Set the thread’s name as the specified name of s.
- void start()
- starts the execution of hte thread.
- String toString()
- returns the string equivalent of a thread.
6
JanGive the use of stop() and yield() method in java.
0 Comments | Posted by kananiprabodh in Java
6
JanWhat is thread? And give two ways to create a thread?
0 Comments | Posted by kananiprabodh in Java
- A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
- You can create a thread by instantiating an object of type Thread.
- Java defines two ways in which this can be accomplished:
• You can implement the Runnable interface.
• You can extend the Thread class, itself.
- By calling isAlive() method on the thread we can determine whether a thread has finished or not.
- This method is defined by Thread, and its general form is shown here:
final boolean isAlive( )
- The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise.
- While isAlive( ) is occasionally useful, the method that you will more commonly use to wait for a thread to finish is called join( ),
Example:
// Using join() to wait for threads to finish.
class NewThread implements Runnable
{
String name; // name of thread
Thread t;
NewThread(String threadname)
{
name = threadname;
t = new Thread(this, name);
System.out.println(“New thread: ” + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run()
{
try {
for(int i = 5; i > 0; i—)
{
System.out.println(name + “: ” + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + ” interrupted.”);
}
System.out.println(name + ” exiting.”);
}
}
class DemoJoin
{
public static void main(String args[])
{
NewThread ob1 = new NewThread(“One”);
NewThread ob2 = new NewThread(“Two”);
NewThread ob3 = new NewThread(“Three”);
System.out.println(“Thread One is alive: ” + ob1.t.isAlive());
System.out.println(“Thread Two is alive: ” + ob2.t.isAlive());
System.out.println(“Thread Three is alive: “+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println(“Waiting for threads to finish.”);
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch (InterruptedException e)
{
System.out.println(“Main thread Interrupted”);
}
System.out.println(“Thread One is alive: ” + ob1.t.isAlive());
System.out.println(“Thread Two is alive: ” + ob2.t.isAlive());
System.out.println(“Thread Three is alive: ” + ob3.t.isAlive());
System.out.println(“Main thread exiting.”);
}
}
- The sleep( ) method causes the thread from which it is called to suspend execution for the specified period of milliseconds.
- It is one method of the Runnable class.
- The pause of threads is accomplished with this method.
- Its general form is shown here:
static void sleep(long milliseconds) throws InterruptedException
- The number of milliseconds to suspend is specified in milliseconds.
- This method may throw an InterruptedException.
- The sleep( ) method has a second form, which allows you to specify the period in terms of milliseconds and nanoseconds:
static void sleep(long milliseconds, int nanoseconds) throws InterruptedException
- This second form is useful only in environments that allow timing periods as short as nanoseconds.
Example:
class CurrentThreadDemo{
public static void main(String args[])
{
Thread t=Thread.currentThread();
System.out.println(“current thread:”+t);
t.setName(“my thread”);
System.out.println(“after name change:=”+t);
try{
for(int n=5;n>0;n++)
{
System.put.println(n);
Thread.sleep(1000);
}
}
catch(InterrupedException e){
System.out.println(“main thread interrupted”);
}
}
}
![[del.icio.us]](http://www.itshala.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.itshala.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://www.itshala.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.itshala.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.itshala.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.itshala.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Twitter]](http://www.itshala.com/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.itshala.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.itshala.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.itshala.com/wp-content/plugins/bookmarkify/email.png)
