1 / 10

Multi-Threading in Java

Multi-Threading in Java. Thread. Thread : a flow of control in a program Process : a running instance of a program including all variables and other states In Java, Application: the Java interpreter starts a thread for the main method.

moshe
Download Presentation

Multi-Threading in Java

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. Multi-Threading in Java

  2. Thread • Thread: a flow of control in a program • Process: a running instance of a program including all variables and other states • In Java, • Application: the Java interpreter starts a thread for the main method. • Applet: the Web browser starts a thread to run the applet. • Java has built-in support for multi-threading.

  3. A typical program Multi-Thread Start Start Task1 Task2 Task1 Task2 Task3 Task3 Stop Stop

  4. Multi-Threading • Multi-threading is the capability of running multiple tasks concurrently within a program. • Multi-threading can make your program more responsive and interactive, and run faster than a non-threaded version. • Issues • Prioritization • Synchronization

  5. Creating Thread • Thread class • Extend Thread class and override the run method • Example class SimpleThread extends Thread {             public void run() {             // work for thread           }        } SimpleThread t = new SimpleThread () ;// create thread t.start(); // begin running thread

  6. Runnable interface • Create object implementing Runnable interface • Pass it to Thread object via Thread constructor • Example class SimpleThread extends Frame implements Runnable{             public void run() {             // work for thread           }        } Thread t = new Thread(new SimpleThread()); // create thread t.start(); // begin running thread

  7. Life cycle of Thread start new I/O complete, time-out, notify, resume new ready yield (dispatch) running blocked I/O, sleep, wait, suspend terminate dead

  8. Overview of the Thread Methods • run():void • start():void – to make a Thread eligible for use • sleep():void – to make a Thread inactive • interrupt():void – to interrupt a running Thread • isAlive():boolean – to check status of a Thread • Other methods • Stop(), setPriority(p: int), wait(), notify()…

  9. Examples • MyThread • Show simple multi-threading • PriThread • Use the method setPriority(p: int)

  10. Thread Synchronization • To avoid resource conflicts, may synchronize method invocation so that only one thread can access a method at a time. • Keyword synchronized • Only one thread at a time to execute statements within synchronized blocks. • All other threads block until the method finishes. • If several synchronized statements are trying to execute on an object at the same time, only one of them is active on the object. • Example: ConcurrenyDemo

More Related