Lec3 - 1 Concurrent Programming in Java Abhik Roychoudhury...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Concurrent Programming in Java Abhik Roychoudhury and Ju Lei CS 3211 National University of Singapore By Abhik & Ju Lei 1 Java Threads Multithreaded execution is an essential feature of the Java platform. Every application has at least one thread — or several, if you count "system" threads that do things like memory management and signal handling. From the application programmer's point of view, you start with just one thread, called the main thread . This thread has the ability to create additional threads. By Abhik & Ju Lei 2 A Time B C Parallelism is not necessary, but possible. The threads can time-share on a processor. Threads Managing Thread objects Each application thread is an instance of the class Thread. In the programming style I describe here: the application directly controls thread creation and management by instantiating the Thread class whenever necessary. By Abhik & Ju Lei 3 An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this: - use the Runnable interface- create a subclass of the Thread class. (see the next two slides for description of these two approaches). Concurrent Thread Execution ` Each thread has a priority ` Initial priority: inherited from its parent thread ` setPriority (int newPriority) ` When multiple threads running on the same processor ` Ready thread with highest priority get executed By Abhik & Ju Lei 4 ` Ready thread with highest priority get executed ` “Randomly” select among threads with same priority Starting a Thread (1) public class HelloRunnable implements Runnable { public void run () { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())). start (); } By Abhik & Ju Lei 5 } } Runnable interface contains a single method run()--- containing code to be executed. Re-define the run() method and pass it to the thread constructor. Starting a Thread (2) public class HelloThread extends Thread { public void run () { System.out.println("Hello from a thread!"); } public static void main(String args[]) { By Abhik & Ju Lei 6 public static void main(String args[]) { (new HelloThread()). start (); } } 2 Common programming mistakes Thread myThread = new Thread(MyRunnable()); mythread.run(); Calling thread will execute the run() method • Treated as normal function call • No new thread is created Not desirable in most situations By Abhik & Ju Lei 7 • Not desirable in most situations Thread myThread = new Thread(MyRunnable()); mythread.start(); A new thread is created (run() is executed in this new thread) Stopping Threads ` Thread normally terminates by returning from its run() method ` Deprecated methods: stop(), suspend(), destroy() etc....
View Full Document

This note was uploaded on 12/02/2011 for the course CS 3211 taught by Professor Dunnowho during the Spring '11 term at National University of Singapore.

Page1 / 8

Lec3 - 1 Concurrent Programming in Java Abhik Roychoudhury...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online