Download presentation
Presentation is loading. Please wait.
1
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new thread to run the target object void run() invoked by JVM to execute the thread void start() starts the thread void interrupt() interrupts this thread boolean isAlive() tests whether the thread is currently running void setPriority(int p) sets priority p ranging from 1 to 10 ) for this thread
2
Thread control methods void join() wait for this thread to finish void yield() causes this thread to temporarily pause and allow other threads to execute boolean isInterrupted() tests if the current thread has been interrupted Thread currentThread() returns a reference to the currently executing thread object
3
Note The Thread class also contains the stop(), suspend() and resume() methods. As of Java 2, these methods are deprecated ( or outdated) because they are known to be inherently unsafe. Instead of using the stop() method, you should assign null to a Thread variable to indicate that it is stopped.
4
yield() You can use yield() method to temporarily release time for other threads. For example, in run method, public void run() { for( int I = 1; I <= lastNum; I++) { System.out.print( “ “+ I); Thread.yield(); } Every time a number is printed, the print100 thread is yield. So each number is followed by some characters
5
Sleep() The sleep(long mills) method puts the thread to sleep for the specified time in milliseconds. Example, in run() method public void run() { for( int I = 1; I <= lastNum; I++) { System.out.print(“ “+ I); try{ if( I>=50) Thread.sleep(1); } catch( InterruptedException ex){ } } } Every time a number (>=50) is printed, the print100 thread is put to sleep for 1 millisecond
6
join() Use join method to force one thread to wait for another thread to finish. For example, in run() method public void run() { for( I = 1; I<= lastNum; I++) { System.out.print( “ “ + I); try { if ( I == 50 ) printA.join(); } catch (InterruptedException ex){ } } } The number from 50 to 100 are printed after thread printA is finished. Who force who? Who wait for who? Current thread wait for thread printA
7
Thread state Thread can be in one of five states: New, Ready, Running, Blocked, finished New: when a thread is created Ready: after calling start(). A thread is runnable but may not be running. The OS had to allocate time Running: A ready thread begins executing Blocked: several reasons, it can be reactivated it may have invoked the join(), sleep() Other thread may have invoked these methods May be waiting for an I/O operation to finish Finished: if it completes the execution of its run() method
8
Find out state isAlive() method is used to find out the state of a thread. It returns true if a thread is in the Ready, Blocked, or Running state; it returns false if a thread is new and has not started or if it is finished Interrupt() method interrupts a thread in the following way: If a thread is currently in the Ready or Running state, its interripted falg is set; If a thread is currently blocked, it is awakened and enters the Ready state, and a java.lang.InterruptedException is thrown
9
Thread priorities Java assigns every thread a priority By default, a thread inherits the priority of the thread that spawned it Increase or decrease the priority of any thread by using setPriority(int) method Priorities are numbers 1 -10 Thread class has int constants MIN_PRIORTY, NORM_PRIORITY and MAX_PRIORITY representing 1,5 and 10. JVM picks highest priority Lower-priority thread can run only when no higher- priority threads are running
10
Thread group A thread group is a set of thread You can group them and perform operation on entire group Use the ThreadGroup constructor to construct a thread group ThreadGroup g = new ThreadGroup(“Thread Group”); This create a group with name Thread Group Place a thread in the group Thread t = new Thread(g, newThreadClass(), “label for the thread”); To find out how many threads in a group are currently running Ssytem.out.println(“The number of thread in the group “+g.activeCount()); Each thread belongs to a thread group. By default, a newly created thread becomes a member of the current thread group that spawned it. To find which group it belongs to, use the getThreadGroup() method
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.