Академический Документы
Профессиональный Документы
Культура Документы
3/10/2018
John Roberts
2
Overview
• What is a thread?
• Thread class
• Runnable interface
• Thread states
• Controlling threads
• Synchronization
3
What is a process?
• Machine code
5
Processes vs. Threads
6
Multithreading
Multiple threads running on multiple processors
Thread 1
Thread 2
Thread 3
Thread 1
Thread 2
Thread 3
7
Overview
• What is a thread?
• Thread class
• Runnable interface
• Thread states
• Controlling threads
• Synchronization
8
Thread class
• Java docs
9
Objective
• Github
10
LetterThread
11
CounterThread
12 What will this print out when I run it? When I run it again?
Client
(make command cleans, compiles, and runs)
1 public class Client {
2 public static void main( String[] args ) {
3 LetterThread a = new LetterThread( 'a' );
4 LetterThread b = new LetterThread( 'b' );
5 CounterThread c = new CounterThread();
6
7 a.start();
8 b.start();
9 c.start();
10 }
11 }
13
Overview
• What is a thread?
• Thread class
• Runnable interface
• Thread states
• Controlling threads
• Synchronization
14
Runnable Interface
• Java docs
15
Objective
• Github
16
extend Thread vs. implement Runnable
• You can only extend only one class, constraining your class
design (rooting the class hierarchy at Thread)
20 Stylistically, I’m not a fan of the temp variables, this could be made
Client
more concise (YMMV):
21
Overview
• What is a thread?
• Thread class
• Runnable interface
• Thread states
• Controlling threads
• Synchronization
22
Thread States
Thread Created
ready stop finished
resume, notify,
start run or notifyAll
stop, or complete stop
new yield, or time
expired
suspend, sleep,
running blocked
or wait
23
Overview
• What is a thread?
• Thread class
• Runnable interface
• Thread states
• Controlling threads
• Synchronization
• void start()
Starts the thread, which causes the run() method to be
invoked. Called by the runnable object in the client class.
25
Controlling Threads
26
Overview
• What is a thread?
• Thread class
• Runnable interface
• Thread states
• Controlling threads
• Synchronization
1 0 newBalance = bank.getBalance() +1
2 0 newBalance = bank.getBalance() +1
3 1 bank.setBalance( newBalance );
4 1 bank.setBalance( newBalance );
28
Piggy Bank
29 https://github.com/sfsu-csc-413-roberts/lecture-11/tree/master/piggy-
Add a Penny Thread, Unsynchronized
bank
30
Synchronized Methods
• Java Tutorial
32
Locks
33 Line 14 means that the *current thread* (main) should wait for
PiggyBank Driver
pennies[i] to complete - this line tells the current thread to wait until all
penny threads have completed execution.
1 public class PiggyBankDriver {
2 public static void main( String[] args ) throws InterruptedException {
3 final int count = 100;
4
5 PiggyBank bank = new PiggyBank();
6 Thread pennies[] = new Thread[ count ];
7
8 for( int i = 0; i < count; i++ ) {
9 pennies[ i ] = new AddAPennyThread( bank );
10 pennies[ i ].start();
11 }
12
13 for( int i = 0; i < count; i++ ) {
14 pennies[ i ].join();
15 }
16
17 System.out.println(
18 "After adding " + count + " pennies, balance is " + bank.getBalance()
19 );
20 }
21 }
34 https://github.com/sfsu-csc-413-roberts/lecture-11/tree/master/piggy-
Add a Penny Thread, with Synchronization
1 public class AddAPennyThreadSynchronized extends Thread {
bank-synchronized
try {
Synchronization is happening on the thread, not on the PiggyBank
object! (We could change the implementation of PiggyBank for more
12 Thread.sleep( 5 );
13 } catch( InterruptedException e ) {
14 System.out.println( "Interrupted" );
15 }
16
17
18 }
bank.setBalance( newBalance ); intuitive access control…)
19
20 public void run() {
21 addAPenny( this.bank );
22 }
23 }
ᐅ make
find . -type f -name '*.class' -delete
javac PiggyBankDriver.java; java PiggyBankDriver
After adding 100 pennies, balance is 1
ᐅ make
find . -type f -name '*.class' -delete
javac PiggyBankDriver.java; java PiggyBankDriver
After adding 100 pennies, balance is 2
ᐅ make
find . -type f -name '*.class' -delete
javac PiggyBankDriver.java; java PiggyBankDriver
After adding 100 pennies, balance is 100