My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
Advertisements
|
Thread in Java
Posted Date: 30 Apr 2008 Resource Type: Articles/Knowledge Sharing Category: Education
|
Posted By: Pournami Member Level: Diamond Rating: Points: 6
|
|
|
|
Thread in Java A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon. When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: · The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. · All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method. There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
The following code would then create a thread and start it running: PrimeThread p = new PrimeThread(143); p.start(); The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following:
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
The following code would then create a thread and start it running: PrimeRun p = new PrimeRun(143); new Thread(p).start(); Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it. Constructor Detail Thread public Thread() Allocates a new Thread object. This constructor has the same effect as Thread(null, null, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer. Method Detail 1. currentThread public static Thread currentThread() Returns a reference to the currently executing thread object. Returns: the currently executing thread. 2. yield public static void yield() Causes the currently executing thread object to temporarily pause and allow other threads to execute. 3. sleep public static void sleep(long millis) throws InterruptedException Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors. Parameters: millis - the length of time to sleep in milliseconds. Throws: InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown. 4. start public void start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method). It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution. 5. run public void run() If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns. Subclasses of Thread should override this method. 6. stop Deprecated public final void stop() Deprecated. This method is inherently unsafe. Forces the thread to stop executing. 7. isAlive public final boolean isAlive() Tests if this thread is alive. A thread is alive if it has been started and has not yet died. Ret true if this thread is alive; false otherwise. 8. suspend Deprecated public final void suspend() Suspends this thread. If the thread is alive, it is suspended and makes no further progress unless and until it is resumed. 9. resume Deprecated public final void resume() Resumes a suspended thread. If the thread is alive but suspended, it is resumed and is permitted to make progress in its execution. 10. join public final void join() throws InterruptedException Waits for this thread to die.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
|