Download Understanding Multithreading and Synchronization in Java and more Lecture notes Java Programming in PDF only on Docsity!
Multithreading
Chapter Goals
- To understand how multiple threads can execute in parallel
- To learn how to implement threads
- To understand race conditions and deadlocks
- To be able to avoid corruption of shared objects by using locks
and conditions
- To be able to use threads for programming animations
Running a Thread
- Implement a class that implements the Runnable interface:
public interface Runnable
void run();
- Place the code for your task into the run method of your class:
public class MyRunnable implements Runnable
public void run()
Task statements
Running a Thread
- Create an object of your subclass:
Runnable r = new MyRunnable();
- Construct a Thread object from the runnable object:
Thread t = new Thread(r);
- Call the start method to start the thread:
t.start();
GreetingRunnable Outline
public class GreetingRunnable implements Runnable
private String greeting;
public GreetingRunnable(String aGreeting)
greeting = aGreeting;
public void run()
Task statements
Thread Action for GreetingRunnable
- Print a time stamp
- Print the greeting
- Wait a second
Running Threads
- sleep puts current thread to sleep for given number of
milliseconds:
Thread.sleep(milliseconds)
- When a thread is interrupted, most common response is to
terminate run
Generic run method
public void run()
try
Task statements
catch (InterruptedException exception)
Clean up, if necessary
ch20/greeting/GreetingRunnable.java (cont.)
22 public void run() 23 { 24 try 25 { 26 for (int i = 1; i <= REPETITIONS; i++) 27 { 28 Date now = new Date(); 29 System.out.println(now + " " + greeting); 30 Thread.sleep(DELAY); 31 } 32 } 33 catch (InterruptedException exception) 34 { 35 } 36 } 37 }
To Start the Thread
- Construct an object of your runnable class:
Runnable t = new GreetingRunnable("Hello World");
- Then construct a thread and call the start method:
Thread t = new Thread(r);
t.start();
ch20/greeting/GreetingThreadRunner.java (cont.)
Program Run:
Mon Dec 28 12:04:46 PST 2009 Hello, World! Mon Dec 28 12:04:46 PST 2009 Goodbye, World! Mon Dec 28 12:04:47 PST 2009 Hello, World! Mon Dec 28 12:04:47 PST 2009 Goodbye, World! Mon Dec 28 12:04:48 PST 2009 Hello, World! Mon Dec 28 12:04:48 PST 2009 Goodbye, World! Mon Dec 28 12:04:49 PST 2009 Hello, World! Mon Dec 28 12:04:49 PST 2009 Goodbye, World! Mon Dec 28 12:04:50 PST 2009 Hello, World! Mon Dec 28 12:04:50 PST 2009 Goodbye, World! Mon Dec 28 12:04:51 PST 2009 Hello, World! Mon Dec 28 12:04:51 PST 2009 Goodbye, World! Mon Dec 28 12:04:52 PST 2009 Goodbye, World! Mon Dec 28 12:04:52 PST 2009 Hello, World! Mon Dec 28 12:04:53 PST 2009 Hello, World! Mon Dec 28 12:04:53 PST 2009 Goodbye, World! Mon Dec 28 12:04:54 PST 2009 Hello, World! Mon Dec 28 12:04:54 PST 2009 Goodbye, World! Mon Dec 28 12:04:55 PST 2009 Hello, World! Mon Dec 28 12:04:55 PST 2009 Goodbye, World!
Thread Scheduler
- Thread scheduler: runs each thread for a short amount of time
(a time slice )
- Then the scheduler activates another thread
- There will always be slight variations in running times -
especially when calling operating system services (e.g. input
and output)
- There is no guarantee about the order in which threads are
executed
Self Check 20.
What would be the result of the program if the main method
called
r1.run();
r2.run();
instead of starting threads?
Answer: The first call to run would print ten “Hello”
messages, and then the second call to run would print ten
“Goodbye” messages
Terminating Threads
- A thread terminates when its run method terminates
- Do not terminate a thread using the deprecated stop method
- Instead, notify a thread that it should terminate:
t.interrupt();
- interrupt does not cause the thread to terminate – it sets a
boolean variable in the thread data structure