1 / 10

Understanding Multithreading in Java

Learn the concept of multithreading in Java, its benefits, and methods of implementation. Includes a demo of matrix multiplication using multiple threads.

reginaldn
Download Presentation

Understanding Multithreading 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. PRACTICAL#07 OBJECTIVE: TO UNDERSTAND MULTITHREADING

  2. THREAD • Simply put, a thread is a program's path of execution. Most programs that run as a single thread, cause problems when multiple events or actions need to occur at the same time.  • When multiple threads execute byte-code instruction sequences in the same program, that action is known as multithreading.  • JVM has to keep track of each thread’s execution.

  3. Let's say, for example, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this.

  4. Benefits • Multithreaded GUI: More responsive • Faster execution • Helpful for networked environment to reduce idle time.

  5. Threads in java There are two ways of creating threads: • Java accomplishes multithreading through its java.lang.Thread class. Each Thread object describes a single thread of execution. You should extend the Thread class and override the run method. • The second method is to implement the Runnable interface and write code for run method.

  6. METHODS

  7. Implementing Runnable • The easiest way to create a thread is to create a class that implements the Runnableinterface. Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run( ), which is declared like this: public void run( )

  8. Matrix Multiplication Code • Execute it and print the time at start and at the end.

  9. Task • Write the same code for matrix multiplication and divide the code into ten threads. Also note the timestamp at the start and end of the program. Give your conclusion.

More Related