1 / 15

Java Threads, I

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!)

saburo
Download Presentation

Java Threads, I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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)

  4. 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.

  5. 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

  6. 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? }

  7. A More Complex Example

  8. An Example Program, cont.

  9. An Example Program, cont.

  10. The Serial Program (for Comparison)

  11. 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

  12. 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…

  13. 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 

  14. 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)

  15. 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

More Related