1 / 86

Java Threads

Java Threads. Representation and Management of Data on the Internet. Threads: Objects with an own line of execution. Main. Thread1. Thread2. Thread3. Multitasking and Multithreading. Multitasking refers to a computer's ability to perform multiple jobs concurrently

pollocka
Download Presentation

Java Threads

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 Representation and Management of Data on the Internet

  2. Threads: Objects with an own line of execution Main Thread1 Thread2 Thread3

  3. Multitasking and Multithreading • Multitasking refers to a computer's ability to perform multiple jobs concurrently • more than one program are running concurrently, e.g., UNIX • A thread is a single sequence of execution within a program • Multithreading refers to multiple threads of control within a single program • each program can run multiple threads of control within it, e.g., Web Browser

  4. Concurrency vs. Parallelism CPU CPU1 CPU2

  5. Threads and Processes CPU Process 1 Process 2 Process 3 Process 4 main run GC

  6. What are Threads Good For? • To maintain responsiveness of an application during a long running task. • To enable cancellation of separable tasks. • Some problems are intrinsically parallel. • To monitor status of some resource (DB). • Some APIs and systems demand it: Swing.

  7. Application Thread • When we execute an application: • The JVM creates a Thread object whose task is defined by the main() method • It starts the thread • The thread executes the statements of the program one by one until the method returns and the thread dies

  8. Multiple Threads in an Application • Each thread has its private run-time stack • If two threads execute the same method, each will have its own copy of the local variables the methods uses • However, all threads see the same dynamic memory (heap) • Two different threads can act on the same object and same static fields concurrently

  9. Creating Threads • There are two ways to create our own Thread object • Subclassing the Thread class and instantiating a new object of that class • Implementing the Runnable interface • In both cases the run() method should be implemented

  10. Implementing Threads • The most simple way: extending the Thread class and overwriting the run() method • Thread is an existing class (no need to import it) • Has a run () method (originally without instructions) whose instructions can be run in parallel to what is currently running • For that the start() method of and instance of the new class should be called • Declaration of the new class • public class MyThread extends Thread { • And somewhere should appear: • public void run() { //instructions to be executed in parallel } • An this is used … • MyThread mt = new MyThread(); • Mt.start()

  11. Example from Java tutorial public class SimpleThread extends Thread { String nombre; public SimpleThread(String str) { nombre = str; } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i+" "+nombre); try { this.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); } } • this.sleep(miliseconds) should appear in a try-catch block

  12. Using the thread public class TwoThreadsTest { public static void main (String[] args) { SimpleThread t1,t2; t1 = SimpleThread("Jamaica"); t2 = SimpleThread("Fiji"); t1.start(); t2.start() } } • start() triggers the parallel execution of run. There are other methods: like join() and interrupt(), suspend(), resume(), stop() are deprecated • See ActiveClock and compare it with ActiveClock2

  13. Implementing Runnable public class RunnableExample implements Runnable { public void run () { for (int i = 1; i <= 100; i++) { System.out.println (“Runnable: ” + i); } } }

  14. A Runnable Object • The Thread object’s run() method calls the Runnable object’s run() method • Allows threads to run inside any object, regardless of inheritance Example – an applet that is also a thread

  15. Starting the Threads public class ThreadsStartExample { public static void main (String argv[]) { //this is previous way new SimpleThread().start (); //this is the new way new Thread(new RunnableExample ()).start (); } } RESULT

  16. ArchServidor.java ArchCliente.java Example 1 File Server 2) Input filename from keyboard 1) Connect 3) Send filename 4) Send bytes 5) Write bytes 3) Read bytes Repeat 3,4,5 until all the file is transmitted This file server is very unstable !!!

  17. About threads and servers • See ActiveClock2 for an example of threads whose execution can be stopped and resumed • This is NOT the way the to stop threads (look at the API why suspend and resume are deprecated) • See ActiveClock3 for a better solution. • CAMBIAR PARA USAR INTERRUPT • Servers normally implement an infinite loop for accepting clients. • In order to allow a clean stop of the server it should be normally be implemented as a thread

  18. File Servers Files: • ArchServidor.java implements a concurrent server, it can be delayed or even killed by a client and all subsequent users will suffer • ArchCliente.java is the client for downloading file from that server • ArchServidor2.java and ServerThread.java implement a concurrent server -> no delay is one client does not type in the filename, if one thread fails the rest will not suffer from it • Note that we don’t need to change a single line form the client program

  19. NoSyncronClient N++ NoSyncronClient NoSyncronClient A number provider Give Number Ask number NoSincronServer Give Number Give Number Ask Number Ask number

  20. Critical regions in Java • A critical region is a piece of code that should be executed by only one thread at the same time to prevent concurrent access to some variables or resources. • Java provides basically two methods for controlling concurrent access to resources • You can declare a whole method as a critical region • Just put synchronized at the beginning of the method declaration • You can use the semaphore of any object • Create any object Object o = new Object() • Start the critical region with synchronized(o) {…….} • Another way to use semaphores in java is with wait() and notify()

  21. Example 2Spaceinvadersusingthreads

  22. Ready queue Newly created threads Currently executed thread • Waiting for I/O operation to be completed • Waiting to be notified • Sleeping • Waiting to enter a synchronized section Scheduling Threads start() I/O operation completes

  23. Thread State Diagram Alive Running new ThreadExample(); while (…) { … } New Thread Runnable Dead Thread thread.start(); run() method returns Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor

  24. Scheduling • Thread scheduling is the mechanism used to determine how runnable threads are allocated CPU time • A thread-scheduling mechanism is either preemptive or nonpreemptive

  25. Preemptive Scheduling • Preemptive scheduling – the thread scheduler preempts (pauses) a running thread to allow different threads to execute • Nonpreemptive scheduling – the scheduler never interrupts a running thread • The nonpreemptive scheduler relies on the running thread to yield control of the CPU so that other threads may execute

  26. Starvation • A nonpreemptive scheduler may cause starvation (runnable threads, ready to be executed, wait to be executed in the CPU a lot of time, maybe even forever) • Sometimes, starvation is also called a livelock

  27. Time-Sliced Scheduling • Time-sliced scheduling – the scheduler allocates a period of time that each thread can use the CPU • when that amount of time has elapsed, the scheduler preempts the thread and switches to a different thread • Nontime-sliced scheduler – the scheduler does not use elapsed time to determine when to preempt a thread • it uses other criteria such as priority or I/O status

  28. Java Scheduling • Scheduler is preemptive and based on priority of threads • Uses fixed-priority scheduling: • Threads are scheduled according to their priority w.r.t. other threads in the ready queue

  29. What is the danger of such scheduler? Java Scheduling • The highest priority runnable thread is always selected for execution above lower priority threads • When multiple threads have equally high priorities, only one of those threads is guaranteed to be executing • Java threads are guaranteed to be preemptive-but not time sliced • Q: Why can’t we guarantee time-sliced scheduling?

  30. Thread Priority • Every thread has a priority • When a thread is created, it inherits the priority of the thread that created it • The priority values range from 1 to 10, in increasing priority

  31. Thread Priority (cont.) • The priority can be adjusted subsequently using the setPriority() method • The priority of a thread may be obtained using getPriority() • Priority constants are defined: • MIN_PRIORITY=1 • MAX_PRIORITY=10 • NORM_PRIORITY=5

  32. Some Notes • Thread implementation in Java is actually based on operating system support • Some Windows operating systems support only 7 priority levels, so different levels in Java may actually be mapped to the same operating system level • What should we do about this?

  33. Daemon Threads • Daemon threads are “background” threads, that provide services to other threads, e.g., the garbage collection thread • The Java VM will not exit if non-Daemon threads are executing • The Java VM will exit if only Daemon threads are executing • Daemon threads die when the Java VM exits

  34. The join() Statement • Used to suspend the execution of one thread until another ends • It is used when we need to complete various sub-tasks before • Most useful if real parallelism can be implemented or attending various clients on the internet • See BuscaThread and learn some spanish ;-)

  35. ThreadGroup • The ThreadGroup class is used to create groups of similar threads. Why is this needed? “Thread groups are best viewed as an unsuccessful experiment, and you may simply ignore their existence.” Joshua Bloch, software architect at Sun

  36. Multithreading Client-Server

  37. Server import java.net.*;import java.io.*; class HelloServer { public static void main(String[] args) { int port = Integer.parseInt(args[0]); try { ServerSocket server = new ServerSocket(port); } catch (IOException ioe) { System.err.println(“Couldn't run “ + “server on port “ + port); return; }

  38. while(true) { try { Socket connection = server.accept(); ConnectionHandler handler = new ConnectionHandler(connection); new Thread(handler).start(); } catch (IOException ioe1) { } }

  39. Connection Handler // Handles a connection of a client to an HelloServer. // Talks with the client in the 'hello' protocol class ConnectionHandler implements Runnable { // The connection with the client private Socket connection; public ConnectionHandler(Socket connection) { this.connection = connection; }

  40. public void run() { try { BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream())); PrintWriter writer = new PrintWriter( new OutputStreamWriter( connection.getOutputStream())); String clientName = reader.readLine(); writer.println(“Hello “ + clientName); writer.flush(); } catch (IOException ioe) {} } }

  41. Client side import java.net.*; import java.io.*; // A client of an HelloServer class HelloClient { public static void main(String[] args) { String hostname = args[0]; int port = Integer.parseInt(args[1]); Socket connection = null; try { connection = new Socket(hostname, port); } catch (IOException ioe) { System.err.println("Connection failed"); return; }

  42. try { BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream())); PrintWriter writer = new PrintWriter( new OutputStreamWriter( connection.getOutputStream())); writer.println(args[2]); // client name String reply = reader.readLine(); System.out.println("Server reply: "+reply); writer.flush(); } catch (IOException ioe1) { } } Note that the Client has not changed from last week

  43. Concurrency • An object in a program can be changed by more than one thread • Q: Is the order of changes that were preformed on the object important?

  44. Race Condition • A race condition –the outcome of a program is affected by the order in which the program's threads are allocated CPU time • Two threads are simultaneously modifying a single object • Both threads “race” to store their value

  45. Race Condition Example Howcanwehave alternatingcolors? Put red pieces Put green pieces

  46. Monitors • Each object has a “monitor” that is a token used to determine which application thread has control of a particular object instance • In execution ofasynchronized method (or block), access to the object monitor must be gained before the execution • Access to the object monitor is queued

  47. Monitor (cont.) • Entering a monitor is also referred to as locking the monitor, or acquiring ownership of the monitor • If a thread A tries to acquire ownership of a monitor and a different thread has already entered the monitor, the current thread (A) must wait until the other thread leaves the monitor

  48. Critical Section • The synchronized methods define critical sections • Execution of critical sections is mutually exclusive. Why?

  49. Example public class BankAccount { private float balance; publicsynchronizedvoid deposit(float amount) { balance += amount; } publicsynchronizedvoid withdraw(float amount) { balance -= amount; } }

  50. Critical Sections t3 t2 t1 deposit() Bank Account

More Related