Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

15 Java Multithreaded Programming

This document discusses different approaches to multithreaded programming in Java. The first approach described is to create separate classes that implement the Runnable interface. An example is provided where a Counter class implements Runnable and is passed to an ExecutorService to run in a background thread. The main App class creates the ExecutorService thread pool and submits multiple Counter instances to it. This allows passing arguments to each thread and keeps the threading logic separate from the main application class.

Uploaded by

apoorv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

15 Java Multithreaded Programming

This document discusses different approaches to multithreaded programming in Java. The first approach described is to create separate classes that implement the Runnable interface. An example is provided where a Counter class implements Runnable and is passed to an ExecutorService to run in a background thread. The main App class creates the ExecutorService thread pool and submits multiple Counter instances to it. This allows passing arguments to each thread and keeps the threading logic separate from the main application class.

Uploaded by

apoorv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

© 2015 Marty Hall

Multithreaded
Programming in Java
Originals of slides and source code for examples: http://courses.coreservlets.com/Course-Materials/java.html
Also see Java 8 tutorial: http://www.coreservlets.com/java-8-tutorial/ and many other Java EE tutorials: http://www.coreservlets.com/
Customized Java training courses (onsite or at public venues): http://courses.coreservlets.com/java-training.html

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
3 Developed and taught by well-known author and developer. At public venues or onsite at your location.

© 2015 Marty Hall

For live Java-related training,


email hall@coreservlets.com
Marty is also available for consulting and development support
Taught by lead author of Core Servlets & JSP, co-author of
Core JSF (4th Ed), & this tutorial. Available at public venues, or
customized versions can be held on-site at your organization.
• Courses developed and taught by Marty Hall
– JSF 2.2, PrimeFaces, servlets/JSP, Ajax, jQuery, Android, Java 7 or 8 programming, GWT, custom mix of topics
– CoursesCustomized Java
available in any state EE Training:
or country. http://courses.coreservlets.com/
Maryland/DC area companies can also choose afternoon/evening courses.
• Courses
Java 7, Java developed and taught Android,
8, JSF 2, PrimeFaces, by coreservlets.com expertsSpring
JSP, Ajax, jQuery, (editedMVC,
by Marty)
RESTful Web Services, GWT, Hadoop.
– Spring MVC, Core Spring, Hibernate/JPA, Hadoop, HTML5, RESTful Web Services
Developed and taught by well-known
Contactauthor and developer. At for
hall@coreservlets.com public venues or onsite at your location.
details
Agenda
• Motivation
• Basic approach
– Make a task list with Executors.newFixedThreadPool
– Add tasks to list with taskList.execute(someRunnable)
• Variations on the theme
– Separate classes that implement Runnable
– Main app implements Runnable
– Inner classes that implement Runnable
– Lambda expressions
• Related topics
– Race conditions and synchronization
– Helpful Thread-related methods
– Advanced topics in concurrency
5

© 2015 Marty Hall

Overview

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
6 Developed and taught by well-known author and developer. At public venues or onsite at your location.
Motivation for Concurrent
Programming
• Pros
– Advantages even on single-processor systems
• Efficiency
– Downloading network data files
• Convenience
– A clock icon
• Multi-client applications
– HTTP Server, SMTP Server
– Many computers have multiple processors
• Find out via Runtime.getRuntime().availableProcessors()
• Cons
– Significantly harder to debug and maintain than single-
7 threaded apps

Steps for
Concurrent Programming
• First, make a task list
ExecutorService taskList =
Executors.newFixedThreadPool(poolSize);
• The poolSize is the maximum number of simultaneous threads.
For many apps, it is higher than the number of tasks, so each
task has a separate thread.
• There are other types of thread pools, but this is simplest
• Second, add tasks to the list (three options)
– Make a separate class that implements Runnable.
• Make instances of this class and start threading via
taskList.execute(new MySeparateRunnableClass(…))
– Have your existing class implement Runnable.
• Start threading via taskList.execute(this)
– Use an inner class.
• taskList.execute(new MyInnerRunnableClass(…))
8
© 2015 Marty Hall

Approach One:
Separate Classes that
Implement Runnable
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
9 Developed and taught by well-known author and developer. At public venues or onsite at your location.

Thread Mechanism One:


Separate Runnable Class
• Make class that implements Runnable
– No import statements needed: Runnable is in java.lang
• Put actions to be performed in run method
– public class MyRunnableClass implements Runnable {
public void run() { … }
}
• Create instance of your class
– Or lots of instances if you want lots of threads
• Pass instance to ExecutorService.execute
– taskList.execute(new MyRunnableClass(…));
• The number of simultaneous threads won’t exceed the
maximum size of the pool.
10
Separate Runnable Class:
Template Code
public class MainClass extends SomeClass {
...
public void startThreads() {
int poolSize = ...;
ExecutorService taskList =
Executors.newFixedThreadPool(poolSize);
for(int i=0; i<something; i++) {
taskList.execute(new SomeTask(...));
}
}
}

public class SomeTask implements Runnable {


public void run() {
// Code to run in the background
}
11 }

Thread Mechanism One:


Example (Continued)
import java.util.concurrent.*;

public class App1 extends SomeClass {


public App1() {
ExecutorService taskList =
Executors.newFixedThreadPool(100);
taskList.execute(new Counter(this, 6));
taskList.execute(new Counter(this, 5)); The shutdown method means that the task list will no
longer accept new tasks (via execute). Tasks already
taskList.execute(new Counter(this, 4)); in the queue will still run. It is not usually necessary
to call shutdown, but in this case, you want the
program to exit after the tasks are completed. If you
taskList.shutdown(); didn’t call shutdown here, you would have to kill the
process with Control-C (command line) or clicking the
} red button (Eclipse), because a background thread
will still be running, waiting for new tasks to be added
to the queue.

public void pause(double seconds) {


try {
Thread.sleep(Math.round(1000.0 * seconds));
} catch (InterruptedException ie) { }
}
}
12
Thread Mechanism One:
Example
public class Counter implements Runnable {
private final App1 mainApp;
private final int loopLimit;

public Counter(App1 mainApp, int loopLimit) {


this.mainApp = mainApp;
this.loopLimit = loopLimit;
}

public void run() {


for(int i=0; i<loopLimit; i++) {
String threadName = Thread.currentThread().getName();
System.out.printf("%s: %s%n", threadName, i);
mainApp.pause(Math.random());
}
}
}

13

Thread Mechanism One:


Example (Continued)
public class App1Test {
public static void main(String[] args) {
new App1();
}
}

14
Thread Mechanism One: Results
pool-1-thread-1: 0
pool-1-thread-2: 0
pool-1-thread-3: 0
pool-1-thread-2: 1
pool-1-thread-2: 2
pool-1-thread-1: 1
pool-1-thread-3: 1
pool-1-thread-2: 3
pool-1-thread-3: 2
pool-1-thread-1: 2
pool-1-thread-1: 3
pool-1-thread-1: 4
pool-1-thread-3: 3
pool-1-thread-2: 4
pool-1-thread-1: 5
15

Pros and Cons of


Separate-Class Approach
• Advantages
– Loose coupling
• Can change pieces independently
• Can reuse Runnable class in more than one application
– Passing arguments
• If you want different threads to do different things, you pass args to
constructor, which stores them in instance variables that run
method uses
– Little danger of race conditions
• You usually use this approach when there is no data shared among
threads, so no need to synchronize.
• Disadvantages
– Hard to access main app.
• If you want to call methods in main app, you must
– Pass reference to main app to constructor, which stores it
– Make methods in main app be public
16
© 2015 Marty Hall

Approach Two: Main


App Implements
Runnable
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
17 Developed and taught by well-known author and developer. At public venues or onsite at your location.

Review of Interfaces: Syntax


• Shape interface
public interface Shape {
public double getArea(); // No body, just specification
}
• Circle class
public class Circle implements Shape {
public double getArea() { some real code }
}
• Note
– You can implement many interfaces
• public class MyClass implements Foo, Bar, Baz { … }

18
Review of Interfaces: Benefits
• Class can be treated as interface type
– public interface Shape {
public double getArea();
}
– public class Circle implements Shape { … }
– public class Rectangle implements Shape { … }

Shape[] shapes =
{ new Circle(…), new Rectangle(…) … };
double sum = 0;
for(Shape s: shapes) {
sum = sum + s.getArea(); // All Shapes have getArea
}
19

Thread Mechanism Two:


Main App Implements Runnable
• Have main class implement Runnable
– Put actions in run method of existing class
• public class MyClass extends Something implements Runnable {

public void run() { … }
}
• Pass the instance of main class to execute
– taskList.execute(this);
• Main differences from previous approach
– Good
• run can easily call methods in main class, since it is in that class
– Bad
• If run accesses any shared data (instance variables), you have
to worry about conflicts (race conditions)
• Very hard to pass arguments, so each task starts off the same
20
Main App Implements Runnable:
Template Code
public class ThreadedClass extends AnyClass
implements Runnable {
public void run() {
// Code to run in background
}

public void startThreads() {


int poolSize = ...;
ExecutorService taskList =
Executors.newFixedThreadPool(poolSize);
for(int i=0; i<someSize; i++) {
taskList.execute(this);
}
}
...
}
21

Thread Mechanism Two:


Example
public class App2 extends SomeClass implements Runnable {
private final int loopLimit;

public App2(int loopLimit) {


this.loopLimit = loopLimit;
ExecutorService taskList =
Executors.newFixedThreadPool(100);
taskList.execute(this);
taskList.execute(this);
taskList.execute(this);
taskList.shutdown();
}

private void pause(double seconds) {


try {
Thread.sleep(Math.round(1000.0 * seconds));
} catch (InterruptedException ie) { }
}
Class continued on next slide
22
Thread Mechanism Two:
Example (Continued)
public void run() {
for(int i=0; i<loopLimit; i++) {
String threadName = Thread.currentThread().getName();
System.out.printf("%s: %s%n", threadName, i);
pause(Math.random());
}
}
}

23

Thread Mechanism Two:


Example (Continued)
public class App2Test {
public static void main(String[] args) {
new App2(5);
}
}

24
Thread Mechanism Two:
Results
pool-1-thread-3: 0
pool-1-thread-1: 0
pool-1-thread-2: 0
pool-1-thread-2: 1
pool-1-thread-3: 1
pool-1-thread-3: 2
pool-1-thread-1: 1
pool-1-thread-2: 2
pool-1-thread-3: 3
pool-1-thread-2: 3
pool-1-thread-1: 2
pool-1-thread-3: 4
pool-1-thread-1: 3
pool-1-thread-2: 4
pool-1-thread-1: 4
25

Pros and Cons of Approach


• Advantages
– Easy to access main app.
• run is already inside main app. Can access any public or
private methods or instance variables.
• Disadvantages
– Tight coupling
• run method tied closely to this application
– Cannot pass arguments to run
• So, you either start a single thread only (quite common), or
all the threads do very similar tasks
– Danger of race conditions
• You usually use this approach specifically because you
want to access data in main application. So, if run modifies
some shared data, you must synchronize.
26
© 2015 Marty Hall

Approach Three: Inner


Class that Implements
Runnable
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
27 Developed and taught by well-known author and developer. At public venues or onsite at your location.

Review of Inner Classes


• Class can be defined inside another class
– Methods in the inner class can access all methods and instance
variables of surrounding class
• Even private methods and variables
• Example
public class OuterClass {
private int count = …;

public void foo(…) {


InnerClass inner = new InnerClass();
inner.bar();
}

private class InnerClass {


public void bar() {
doSomethingWith(count);
}
}
}
28
Thread Mechanism Three:
Runnable Inner Class
• Have inner class implement Runnable
– Put actions in run method of inner class
• public class MyClass extends Whatever {

private class SomeInnerClass implements Runnable {
public void run() { … }
}
}
• Pass instances of inner class to execute
– taskList.execute(new SomeInnerClass(…));

29

Inner Class Implements


Runnable: Template Code
public class MainClass extends AnyClass {
public void startThreads() {
int poolSize = ...;
ExecutorService taskList =
Executors.newFixedThreadPool(poolSize);
for(int i=0; i<someSize; i++) {
taskList.execute(new RunnableClass(...));
}
}
...
private class RunnableClass implements Runnable {
public void run() {
// Code to run in background
}
}
}
30
Minor Variation:
Anonymous Inner Class
public class MainClass extends AnyClass {
public void startThreads() {
int poolSize = ...;
ExecutorService taskList =
Executors.newFixedThreadPool(poolSize);
for(int i=0; i<someSize; i++) {
taskList.execute(new Runnable() {
public void run() {
...
}
});
}
}
}

31

Thread Mechanism Three:


Example
public class App3 extends SomeClass {
public App3() {
ExecutorService taskList =
Executors.newFixedThreadPool(100);
taskList.execute(new Counter(6));
taskList.execute(new Counter(5));
taskList.execute(new Counter(4));
taskList.shutdown();
}

private void pause(double seconds) {


try {
Thread.sleep(Math.round(1000.0 * seconds));
} catch (InterruptedException ie) { }
}

Class continued on next slide


32
Thread Mechanism Three:
Example (Continued)
private class Counter implements Runnable { // Inner class
private final int loopLimit;

public Counter(int loopLimit) {


this.loopLimit = loopLimit;
}

public void run() {


for(int i=0; i<loopLimit; i++) {
String threadName = Thread.currentThread().getName();
System.out.printf("%s: %s%n", threadName, i);
pause(Math.random());
}
}
}
}

You can also use anonymous inner classes. This is not different enough to warrant a separate
33 example here, especially since we showed examples in the section on event handling.

Thread Mechanism Three:


Example (Continued)
public class App3Test {
public static void main(String[] args) {
new App3();
}
}

34
Thread Mechanism Three:
Results
pool-1-thread-2: 0
pool-1-thread-1: 0
pool-1-thread-3: 0
pool-1-thread-3: 1
pool-1-thread-1: 1
pool-1-thread-1: 2
pool-1-thread-2: 1
pool-1-thread-3: 2
pool-1-thread-3: 3
pool-1-thread-1: 3
pool-1-thread-1: 4
pool-1-thread-1: 5
pool-1-thread-2: 2
pool-1-thread-2: 3
pool-1-thread-2: 4
35

Pros and Cons of Approach


• Advantages
– Easy to access main app.
• Methods in inner classes can access any public or private
methods or instance variables of outer class.
– Can pass arguments to run
• As with separate classes, you pass args to constructor,
which stores them in instance variables that run uses
• Disadvantages
– Tight coupling
• run method tied closely to this application
– Danger of race conditions
• You usually use this approach specifically because you
want to access data in main application. So, if run modifies
some shared data, you must synchronize.
36
© 2015 Marty Hall

Preview of Approach
Four: Lambda
Expressions
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
37 Developed and taught by well-known author and developer. At public venues or onsite at your location.

Preview of Lambdas
• Anonymous inner class
taskList.execute(new Runnable() {
@Override
public void run() {
doSomeTask(…);
}
});

• Lambda equivalent
taskList.execute(() -> doSomeTask(…));

Lambda expressions are covered in later tutorial section.


38 See http://www.coreservlets.com/java-8-tutorial/
© 2015 Marty Hall

Summary of
Approaches

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
39 Developed and taught by well-known author and developer. At public venues or onsite at your location.

Pros and Cons


• Separate class that implements Runnable
– Cannot easily access data in main class (and only public data)
– Can pass args to run (indirectly via constructor and instance variables)
– Usually no worry about race conditions
• Main class implements Runnable
– Can easily access data in main class
– Cannot pass args to run
– Must worry about race conditions
• Inner class implements Runnable
– Can easily access data in main class
– Can pass args to run (indirectly via constructor and instance variables)
– Must worry about race conditions
• Lambdas
– Can easily access data in main class
– Cannot pass args to run (no instance variables)
40
– Must worry about race conditions
Example: Template for a
Multithreaded Network Server
import java.net.*;
import java.util.concurrent.*;
import java.io.*;

public class MultithreadedServer {


private int port;

public MultithreadedServer(int port) {


this.port = port;
}

public int getPort() {


return(port);
}

41

MultithreadedServer.java
(Continued)
public void listen() {
int poolSize =
100 * Runtime.getRuntime().availableProcessors();
ExecutorService taskList =
Executors.newFixedThreadPool(poolSize);
try {
ServerSocket listener = new ServerSocket(port);
Socket socket;
while(true) { // Run until killed
socket = listener.accept();
taskList.execute(new ConnectionHandler(socket));
}
} catch (IOException ioe) {
System.err.println("IOException: " + ioe);
ioe.printStackTrace();
The later sections on network programming will give details on ServerSocket and Socket.
} But the basic idea is that the server accepts a connection and then puts it in the queue of
tasks so that it can be handled in a background thread. The network servers section will
42 }} give a specific example of this code applied to making an HTTP server.
ConnectionHandler.java
public class ConnectionHandler implements Runnable {
private Socket socket;

public ConnectionHandler(Socket socket) {


this.socket = socket;
}

public void run() {


try {
handleConnection(socket);
} catch(IOException ioe) {
System.err.println("IOException: " + ioe);
ioe.printStackTrace();
}
}

public void handleConnection(Socket socket)


throws IOException{
// Do something with socket
}
}
43

© 2015 Marty Hall

Race Conditions and


Synchronization

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
44 Developed and taught by well-known author and developer. At public venues or onsite at your location.
Race Conditions: Example
public class RaceConditionsApp implements Runnable {
private final static int LOOP_LIMIT = 5;
private final static int POOL_SIZE = 10;
private int latestThreadNum = 0;

public RaceConditionsApp() {
ExecutorService taskList;
taskList = Executors.newFixedThreadPool(POOL_SIZE);
for (int i=0; i<POOL_SIZE; i++) {
taskList.execute(this);
}
}

45

Race Conditions: Example


(Continued)
public void run() {
int currentThreadNum = latestThreadNum;
System.out.println("Set currentThreadNum to "
+ currentThreadNum);
latestThreadNum = latestThreadNum + 1;
for (int i=0; i<LOOP_LIMIT; i++) {
doSomethingWith(currentThreadNum);
}
}

private void doSomethingWith(int threadNumber) {


// Blah blah
}

• What’s wrong with this code?


46
Race Conditions: Result
• Expected Output • Occasional Output
Set currentThreadNum to 0 Set currentThreadNum to 0
Set currentThreadNum to 1 Set currentThreadNum to 1
Set currentThreadNum to 2 Set currentThreadNum to 2
Set currentThreadNum to 3 Set currentThreadNum to 3
Set currentThreadNum to 4 Set currentThreadNum to 4
Set currentThreadNum to 5 Set currentThreadNum to 5
Set currentThreadNum to 6 Set currentThreadNum to 5
Set currentThreadNum to 7 Set currentThreadNum to 7
Set currentThreadNum to 8 Set currentThreadNum to 8
Set currentThreadNum to 9 Set currentThreadNum to 9

In older Java versions, the error showed up rarely: only about 1 in 50 times. In newer Java
versions (that give each thread a smaller time slice and where the underlying computer is
faster), it happens often. There is another version of the code in the Eclipse project that even on
47 new Java versions, manifests the problem only about 1 in 50 times.

Race Conditions: Solution?


• Do things in a single step
public void run() {
int currentThreadNum = latestThreadNum++;
System.out.println("Set currentThreadNum to "
+ currentThreadNum);
for (int i=0; i<LOOP_LIMIT; i++) {
doSomethingWith(currentThreadNum);
}
}

This “solution” does not fix the problem. In some ways, it makes it worse!

48
Arbitrating Contention for
Shared Resources
• Synchronizing a section of code
synchronized(someObject) {
code
}

• Fixing the previous race condition


public void run() {
synchronized(this) {
int currentThreadNum = latestThreadNum;
System.out.println("Set currentThreadNum to "
+ currentThreadNum);
latestThreadNum = latestThreadNum + 1;
}
for (int i=0; i<LOOP_LIMIT; i++) {
doSomethingWith(currentThreadNum);
}
}
49

Arbitrating Contention for


Shared Resources
• Synchronizing a section of code
synchronized(someObject) {
code
}
• Normal interpretation
– Once a thread enters that section of code, no other thread
can enter until the first thread exits.
• Stronger interpretation
– Once a thread enters that section of code, no other thread
can enter any section of code that is synchronized using
the same “lock” object
• If two pieces of code say “synchronized(blah)”, the
50
question is if the blah’s are the same object instance.
Arbitrating Contention for
Shared Resources
• Synchronizing an entire method

public synchronized void someMethod() {


body
}

• Note that this is equivalent to

public void someMethod() {


synchronized(this) {
body
}
}
51

© 2015 Marty Hall

Helpful Thread-Related
Methods

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
52 Developed and taught by well-known author and developer. At public venues or onsite at your location.
Methods in Thread Class
• Thread.currentThread()
– Gives instance of Thread running current code
• Thread.sleep(milliseconds)
– Puts calling code to sleep. Useful for non-busy waiting in
all kinds of code, not just multithreaded code. You must
catch InterruptedException, but you can ignore it:
• try { Thread.sleep(someMilliseconds); }
catch (InterruptedException ie) { }
– See also TimeUnit.SECONDS.sleep,
TimeUnit.MINUTES.sleep, etc.
• Same idea except takes sleep time in different units.
• someThread.getName(), someThread.getId()
– Useful for printing/debugging, to tell threads apart
53

Methods in ExecutorService
Class
• execute(Runnable)
– Adds Runnable to the queue of tasks
• shutdown
– Prevents any more tasks from being added with execute (or
submit), but lets current tasks finish.
• shutdownNow
– Attempts to halt current tasks. But author of tasks must have
them respond to interrupts (ie, catch InterruptedException), or
this is no different from shutdown.
• awaitTermination
– Blocks until all tasks are complete. Must shutdown() first.
• submit, invokeAny, invokeAll
– Variations that use Callable instead of Runnable. See next
slide on Callable.
54
Callable
• Runnable
– “run” method runs in background. No return values, but
run can do side effects.
– Use “execute” to put in task queue
• Callable
– “call” method runs in background. It returns a value that
can be retrieved after termination with “get”.
– Use “submit” to put in task queue.
– Use invokeAny and invokeAll to block until value or
values are available
• Example: you have a list of links from a Web page and
want to check status (404 vs. good). Submit them to a task
queue to run concurrently, then invokeAll will let you see
55
return values when all links are done being checked.

Lower-Level Threading
• Use Thread.start(someRunnable)
– Implement Runnable, pass to Thread constructor, call start
• Thread t = new Thread(someRunnable);
• t.start();
– About same effect as taskList.execute(someRunnable), except
that you cannot put bound on number of simultaneous threads.
– Mostly a carryover from pre-Java-5 days; still widely used.
• Extend Thread
– Put run method in Thread subclass, instantiate, call start
• SomeThread t = new SomeThread(…);
• t.start();
– A holdover from pre-Java-5; has little use in modern Java
applications.
56
© 2015 Marty Hall

Advanced Topics

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
57 Developed and taught by well-known author and developer. At public venues or onsite at your location.

Types of Task Queues


• Executors.newFixedThreadPool(nThreads)
– Simplest and most widely used type. Makes a list of tasks
to be run in the background, but with caveat that there are
never more than nThreads simultaneous threads running.
• Executors.newScheduledThreadPool
– Lets you define tasks that run after a delay, or that run
periodically. Replacement for pre-Java-5 “Timer” class.
• Executors.newCachedThreadPool
– Optimized version for apps that start many short-running
threads. Reuses thread instances.
• Executors.newSingleThreadExecutor
– Makes queue of tasks and executes one at a time
• ExecutorService (subclass) constructors
58
– Lets you build FIFO, LIFO, and priority queues
Stopping a Thread
public class SomeTask implements Runnable {
private volatile boolean running;

public void run(){


Compilers on multiprocessor systems often do optimizations that prevent
running = true; changes to variables from one thread from being seen by another thread. To
guarantee that other threads see your changes, either use synchronized
while (running) { methods, declare the variable “volatile”, or use AtomicBoolean.

...
}
doCleanup();
}

public void setRunning(boolean running) {


this.running = running;
}
}
59

Nasty Synchronization Bug


public class Driver { Separate class or inner class. But this
problem does not happen when you
public void startThreads() { put “this” here.

for(…) {
taskList.execute(new SomeHandler(...));
}}}

public class SomeHandler implements Runnable {


public synchronized void doSomeOperation() {
accessSomeSharedObject();
This keyword has no effect
} whatsoever in this context! Why?
...
public void run() {
while(someCondition) {
doSomeOperation(); // Accesses shared data
doSomeOtherOperation();// No shared data
}
}
60 }
Synchronization Solution
• Solution 1: synchronize on outer class
– If your handler is an inner class, not a separate class

public OuterClassName {
public void someMethod() {
...
taskList.execute(new SomeHandler(...));
}

private class SomeHandler implements Runnable {


public void run() { ... }

public void doSomeOperation() {


synchronized(OuterClassName.this) {
accessSomeSharedObject();
}
}
61
}

Synchronization Solutions
• Solution 2: synchronize on the shared data
public void doSomeOperation() {
synchronized(someSharedObject) {
accessSomeSharedObject();
}
}

• Solution 3: synchronize on the class object


public void doSomeOperation() {
synchronized(SomeHandler.class) {
accessSomeSharedObject();
}
}

• Note that if you use “synchronized” for a static method, the lock
is the corresponding Class object, not this
62
Synchronization Solution
(Continued)
• Solution 4: synchronize on arbitrary object
public class SomeHandler implements Runnable{
private static Object lockObject
= new Object();
...
public void doSomeOperation() {
synchronized(lockObject) {
accessSomeSharedObject();
}
}
...
}

– Why doesn’t this problem usually occur with thread


mechanism two (with run method in main class)?
63

Determining Maximum Thread


Pool Size
• In most apps, a reasonable guess is fine
int maxThreads = 100;
ExecutorService taskList =
Executors.newFixedThreadPool(maxThreads);
• If you need more precise values, you can
use equation
maxThreads = numCpus * targetUtilization *
(1 + avgWaitTime/avgComputeTime)

• Compute numCpus with


Runtime.getRuntime().availableProcessors()
• targetUtilization is from 0.0 to 1.0
• Find ratio of wait to compute time with profiling
64 • Equation taken from Java Concurrency in Practice
Other Advanced Topics
• wait/waitForAll
– Releases the lock for other threads and suspends itself (placed in a
wait queue associated with the lock)
– Very important in some applications, but very, very hard to get
right. Try to use the newer Executor services if possible.
• notify/notifyAll
– Wakes up all threads waiting for the lock
– A notified thread doesn’t begin immediate execution, but is placed
in the runnable thread queue
• Concurrency utilities in java.util.concurrency
– Advanced threading utilities including semaphores, collections
designed for multithreaded applications, atomic operations, etc.
• Debugging thread problems
– Use JConsole (bundled with Java 5; officially part of Java 6+)
• http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html
65

© 2015 Marty Hall

Wrap-Up

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
66 Developed and taught by well-known author and developer. At public venues or onsite at your location.
References
• Books
– Java Concurrency in Practice (Goetz, et al)
• Brian Goetz is now the Java SE spec lead
– Chapter 10 (“Concurrency”) of Effective Java, 2nd Ed
(Josh Bloch)
• Effective Java is the all-time best Java practices book
– Java Threads (Oak and Wong)
• Online references
– Lesson: Concurrency (Oracle Java Tutorial)
• http://docs.oracle.com/javase/tutorial/essential/concurrency/
– Jacob Jenkov’s Concurrency Tutorial
• http://tutorials.jenkov.com/java-concurrency/index.html
– Lars Vogel’s Concurrency Tutorial
• http://www.vogella.de/articles/JavaConcurrency/article.html
67

Summary
• Basic approach
ExecutorService taskList =
Executors.newFixedThreadPool(poolSize);
• Three variations
– taskList.execute(new SeparateClass(…));
– taskList.execute(this); Really four variations, with lambdas being
– taskList.execute(new InnerClass(…)); the fourth. But lambda expressions are not
covered in detail yet.

• Handling shared data


synchronized(referenceSharedByThreads) {
getSharedData();
modifySharedData();
}
68
doOtherStuff();
© 2015 Marty Hall

Questions?
More info:
http://courses.coreservlets.com/Course-Materials/java.html – General Java programming tutorial
http://www.coreservlets.com/java-8-tutorial/ – Java 8 tutorial
http://courses.coreservlets.com/java-training.html – Customized Java training courses, at public venues or onsite at your organization
http://coreservlets.com/ – JSF 2, PrimeFaces, Java 7 or 8, Ajax, jQuery, Hadoop, RESTful Web Services, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training
Many additional free tutorials at coreservlets.com (JSF, Android, Ajax, Hadoop, and lots more)

Customized Java EE Training: http://courses.coreservlets.com/


Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
69 Developed and taught by well-known author and developer. At public venues or onsite at your location.

You might also like