15-Multithreaded-Programming

15-Multithreaded-Programming - 2006 Marty Hall...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
2 J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, Hibernate, AJAX, Java 5, etc. Developed and taught by well-known author and developer. At public venues or onsite at your location. © 2006 Marty Hall Multithreaded Programming in Java 3 Developed and taught by well-known author and developer. At public venues or onsite at your location. © 2006 Marty Hall For live Java training, please see training courses at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF, AJAX, Java 5, etc. Taught by the author of Core Servlets and JSP , More Servlets and JSP , and this tutorial. Available at public venues, or customized versions can be held on-site at your organization.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 J2EE training: http://courses.coreservlets.com Agenda Why threads? Approaches for starting threads Separate class approach Callback approach Solving common thread problems Synchronizing access to shared resources Thread life cycle Stopping threads 5 J2EE training: http://courses.coreservlets.com Concurrent Programming Using Java Threads Motivation Efficiency Downloading network data files Convenience A clock icon Multi-client applications HTTP Server, SMTP Server Caution Significantly harder to debug and maintain Two Main Approaches: Make a self-contained subclass of Thread with the behavior you want Implement the Runnable interface and put behavior in the run method of that object
Background image of page 2
6 J2EE training: http://courses.coreservlets.com Thread Mechanism One: Making a Thread Subclass Create a separate subclass of Thread No import statements needed: Thread is in java.lang Put the actions to be performed in the run method of the subclass public void run() { … } Create an instance of your Thread subclass Or lots of instances if you want lots of threads Call that instance’s start method You put the code in run , but you call start ! 7 J2EE training: http://courses.coreservlets.com Thread Mechanism One: Making a Thread Subclass public class DriverClass extends SomeClass { ... public void startAThread() { // Create a Thread object ThreadClass thread = new ThreadClass() ; // Start it in a separate process thread.start() ; } } public class ThreadClass extends Thread { public void run() { // Thread behavior here } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 J2EE training: http://courses.coreservlets.com Thread Mechanism One: Example public class Counter extends Thread { private static int totalNum = 0; private int currentNum, loopLimit; public Counter(int loopLimit) { this.loopLimit = loopLimit; currentNum = totalNum++; } private void pause(double seconds) { try { Thread.sleep(Math.round(1000.0*seconds)); } catch(InterruptedException ie) {} } ... 9 J2EE training: http://courses.coreservlets.com Thread Mechanism One: Example (Continued) ... /** When run finishes, the thread exits. */ public void run() { for(int i=0; i<loopLimit; i++) { System.out.println( "Counter " + currentNum + ": " + i ); pause(Math.random()); // Sleep for up to 1 second } } }
Background image of page 4
10 J2EE training: http://courses.coreservlets.com Thread Mechanism One: Example (Continued) public class CounterTest { public static void main(String[] args) {
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

15-Multithreaded-Programming - 2006 Marty Hall...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online