1 / 14

Multithreading

Multithreading. A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program concurrently. These threads can be executed simultaneously in multiprocessor systems.

ifama
Download Presentation

Multithreading

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. Multithreading • A thread is the flow of execution, from beginning to end, of a task in a program. • With Java, you can launch multiple threads from a program concurrently. • These threads can be executed simultaneously in multiprocessor systems. • In single processor system, multiple threads share CPU time

  2. Runnable interface • When your program executes as an application, Java interpreter starts a thread for the main method. • When your program executes as an applet, the web browser starts a thread to run the applet. • You can create additional threads to run concurrent task in the program. • Each new thread is an object of a class that implements the Runnable interface or extends a class that implements the Runnable interface. • You can create threads either by extending the Thread class or by implementing the Runnable interface. • Both Thread and Runnable are defined in the java.lang

  3. Creating Threads by Extending Thread class • Thread class contains • Constructors • Many useful method controlling the thread • To create and run a thread • Define a class that extends Thread • Must override the run() method, which tells the system how the thread will be executed when it runs • Create an object running on the thread

  4. Figure // custom thread class public class myThread extends Thread { // constructor public myThread() { ….. } // override the run method public void run() { // tell system how to run … } … }// end of class myThread // client class public class client { ….. // some method public void someMethod() { // create a thread myThread th = new myThread(); // start a thread; th.start(); … } … }// end of class myThread

  5. Example • Problem: write a program that creates and runs three threads: • The first thread prints the letter a one hundred times • The second thread prints the letter b one hundred times • The third thread prints the integers 1 through 100. • Solution: • The program has three independent threads. • To run them concurrently, it needs to create a runnable object for each thread • The first two thread have similarity, we can define in one thread class and create two different thread for letter a and b.

  6. Program code // TestThread.java public class TestThread{ public static void main(String [] args) { // create threads PrintChar printA = new PrintChar(‘a’,100); PrintChar printB = new PrintChar(‘b’, 100); PrintNum print100 = new PrintNum (100); // start threads printA.start(); printB.start(); print100.start(); } // end of main }// end of class TestThread

  7. Program code - continued class PrintChar extends Thread{ private char charToPrint; private int times; // construcotr public PrintChar(char x, int n){ charToPrint = x; times = n; } // override run() method public void run() { for ( int I = 0; I < times; I++) System.out.print (charToPrint); }

  8. Program code - continued // the thread class for printing number from 1-n class PrintNum extends Thread { private int lastNum; // constructor public PrintNum( int n) { lastNum = n; } // override run() method public void run(){ for ( int I = 0; I < ladtNum; I++) System.out.print( I + “ “); } // end of run } // end of class

  9. Implementing Runnable Interface • Java have no multiple inheritance • Java provides Runnable interface as alternative to the Thread class • Runnable interface just contain the run() method • You need to implement this method to tell system how your thread is going to run

  10. // client class public class client { ….. // some method public void someMethod() { // create an instance of myThread myThread myth = new myThread(…); // create a thread Thread th = new Thread(myth); // start a thread; th.start(); … } … }// end of class myThread // custom thread class public class myThread implements Runnable { // constructor public myThread() { ….. } // implement the run method public void run() { // tell system how to run … } … }// end of class myThread

  11. Program Code public class testRunnable{ // create threads Thread printA = new Thread(new PrintChar(‘a’,100)); Thread printB = new Thread(new PrintChar(‘b’,100)); Thread print100 = new Thread (new PrintNum(100)); public static void main(String []args){ new TestRunnable(); }

  12. Program code - continued public TestRunnable(){ printA.start(); printB.start(); print100.start(); } // thread class for printing letter a or b class PrintChar implements Runnable{ private char charToPrint; private int times; public PrintChar(char c, int t){ charToPrint = c; times = t; }

  13. Program code - continued // override the run method public void run(){ for ( int i = 0; i <times; i++) System.out.print(charToPrint); } }// end of class

  14. Program code -continued // class for printing number from 1 to n Class PrintNum implements Runnable{ private int lastNum; public PrintNum(int n){ lastNum = n; } // override run() Public void run(){ for ( int I = 0; I < lastNum; i++){ System.out.print(“ “ + i); } } }

More Related