1 / 10

More on Thread API

More on Thread API. public void interrupt(); Receiving thread is interrupted. public boolean isInterrupted() Tests whether this thread has been interrupted. public static boolean interrupted()

Download Presentation

More on Thread API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Thread API 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. 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 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); Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads

  5. JVM threads priorities Priority Thread name 5 main 8 Finalizer (for gc) 10 Reference handler 5 signal dispatcher 5 AWT Windows 6 AWT-Event-Queue-0 5 SunToolKit.PostEventQueue-0 4 Screen updater 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. 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. 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. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads

  9. Threads states on a single processor State block Interrupted Description Running currently running Ready to run waiting 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. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads

  10. New tread Chosen by scheduler To run running start() yield() Ready to run scheduler swaps it out unblocks Sleeping Waiting Blocked on I/O Blocked on Sync thread blocks Transition states Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads

More Related