150 likes | 264 Views
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!)
E N D
Java Threads, I Mt Hebron CS Club http://mthcompsci.wordpress.com/ Nov 2013
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
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)
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.
These “Runnable” parameters can be the classes which you define! (as long as you ‘implement Runnable’ in your class) Source: Oracle Documentation: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
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? }
Some Notes • The Serial program has the same code as the Threaded one, but it never invokes any Thread methods. • 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 terminates the moment it prints the output
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…
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
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)
To Be Continued… Sources / For more Info: • Thanks for Attending! • If you have questions, feel free to ask • Next meeting: next Thursday afternoon • Main source: http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html • http://www.seas.gwu.edu/~simhaweb/java/lectures/module12/module12.html • http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html