java threads, i

15
Java Threads, I Mt Hebron CS Club http://mthcompsci.wordpress.com/ Nov 2013

Upload: saburo

Post on 20-Feb-2016

47 views

Category:

Documents


2 download

DESCRIPTION

Java Threads, I. Mt Hebron CS Club http://mthcompsci.wordpress.com/ Nov 2013. Fundamental Idea. Processes L arge, serialized, and have their own data storage Have you ever asked yourself : How can we run two things at once in Java? (This is a fair bit complex!) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Java Threads, I

Java Threads, I

Mt Hebron CS Clubhttp://mthcompsci.wordpress.com/

Nov 2013

Page 2: Java Threads, I

Fundamental Idea• Processes– Large, serialized, and have their own

data storage– Have you ever asked yourself :– How can we run two things at once in

Java?• (This is a fair bit complex!)• Threads can run simultaneously and

share identical data from a heap/stack

Page 3: Java Threads, I

A static example – pausing Code

• Thread.sleep (long ms);• ^This method can cause a thread to

pause for the specified amount of time, in milliseconds

• Danger – interruption (threads could be interrupted by another command)

Page 4: Java Threads, I

Implementation in Java• Thread Object is built-in to Java• Runnable – interface used when specifying

the class given to a thread to run (“allocating”)

• In this class, which you’re giving a thread to run, you’ll need to override the run() and start() methods to say what you want this thread to run.

• Reserved word synchronized• ^This is for more advanced threaded

programming; we may get to this topic later.

Page 5: Java Threads, I

Source: Oracle Documentation: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

These “Runnable” parameters can be the classes which you define!(as long as you ‘implement Runnable’ in your class)

Page 6: Java Threads, I

Examples – Starting Simple• Make a program to print:

“Hello from a threaded Object!”• …using a Thread Constructor and a

second class.• Remember! JVM invokes the class’s

run() method when you invoke [threadName].start()public class Driver

{ public static void main (String[] args) { // Use a Constructor of thread // invoke: [thread’s identifier].start(); /* Then wait for a second, or use join() method */ }}

public class PrintHello implements Runnable{

}

public PrintHello (){}public void run (){ // What are you trying to do?}

Page 7: Java Threads, I

A More Complex Example

Page 8: Java Threads, I

An Example Program, cont.

Page 9: Java Threads, I

An Example Program, cont.

Page 10: Java Threads, I

The Serial Program (for Comparison)

Page 11: Java Threads, I

Some Notes• Each Thread has been

given a separate class to run (this is why each class was called ‘Runnable’)

• The threaded program waits about 200 seconds for both threads to complete before finally terminating

• The Serial program has the same code as the Threaded one, but it never invokes any Thread methods.• The Serial program terminates the moment it prints the output

Page 12: Java Threads, I

More Important Points• Key point: how many threads does this

program have?• Answer: 3• There is a main thread that begins, and two

others are created manually in this program.• You may be wondering:• This is all interesting, but how can I (or, say,

some company) use this feature as a benefit?• I ran some tests to determine execution time…

Page 13: Java Threads, I

Results of Comparison

• Threaded programming can be 20-30% quicker in my example (perhaps more efficient in other examples!)

• Execution time – very important, in competitions and in real programming jobs

Page 14: Java Threads, I

Implementation in Java, II• If two different threads try to access/act on

the same data,– Memory consistency error could occur.

• synchronized methods try to reduce this problem

• ‘Liveness’ – efficiency• Issues possible with liveness

w/threads:– deadlock (≥2 threads waiting for each other to act),– livelock (≥2 threads both continuously replying to each

other),– Starvation (one thread has absolute access to data for

long amounts of time)