Download presentation
Presentation is loading. Please wait.
Published byLuke Robbins Modified over 9 years ago
1
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API
2
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 2 public void interrupt(); Receiving thread is interrupted. public boolean isInterrupted() Tests whether this thread has been interrupted. public static boolean interrupted() Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it). static void yield() Causes the currently executing thread object to temporarily pause and allow other threads to execute.
3
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 3 Deprecated methods: –public final void suspend() –public final void resume() –public final void stop() A thread is considered alive just after the start() method is invoked, until its run() method naturally terminates; if you choose to stop a thread use interrupt(). Read example : BestReplacement.java in hyde/code
4
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 4 Deamon threads Used for background supporting tasks, and needed while normal non-deamon threads are running. When the JVM detects that only reamining threads are deamon threads it exits. Used to manage behind-the-scenes processing needed by all other threads. After a thread is created it can be set to deamon with –public void setDeamon(Boolean flag);
5
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 5 JVM threads priorities Priority Thread name 5main 8Finalizer (for gc) 10Reference handler 5signal dispatcher 5AWT Windows 6AWT-Event-Queue-0 5SunToolKit.PostEventQueue-0 4Screen updater
6
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 6 Assigning priorities Consider the relative priorities of JVM threads to be sure your threads do not overwhelm their operations. By default, when a new thread is created it runs at same priority as the thread that created it. Most threads are created by the main, so are at priority 5. There are times when you want to raise or lower the priority.
7
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 7 API Priority public int getPriority() public final void setPriority(int newPriority) Changes the priority of this thread. First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException. Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group. static int MAX_PRIORITY The maximum priority that a thread can have. static int MIN_PRIORITY The minimum priority that a thread can have. static int NORM_PRIORITY The default priority that is assigned to a thread.
8
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 8 Priority Tip When assigning priorities to threads in your app, use the higher priorities only for threads that block frequently (sleep, wait, block on I/O, even yield). CPU intensive calculations should be done with a medium-to-low priority thread to ensure that processor is not hogged. Avoid setting Thread.MAX_PRIORITY unless thread spends nearly all of its time blocked or it is very short-lived.
9
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 9 Threads states on a single processor State block InterruptedDescription Runningcurrently running Ready to runwaiting for turn on processor Sleeping X X interrupted will move to ready to run after certain amount of time has elapsed or after being interrupted. Waiting X X will move to ready to run after being notified, after timing out, or after being interrupted. Blocked on I/O X will move to ready to run after I/O condition changes. Blocked on sync X will move to ready to run when lock is acquired.
10
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 10 Transition states New tread Ready to run running Sleeping Waiting Blocked on I/O Blocked on Sync start() Chosen by scheduler To run yield() scheduler swaps it out thread blocks unblocks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.