Download presentation
Presentation is loading. Please wait.
Published byGwen Rogers Modified over 9 years ago
1
Critical Reference An occurrence of a variable v is defined to be critical reference: a. if it is assigned to in one process and has an occurrence in another process. b. if it has an occurrence in an expression in one process and is assigned to in another.
2
Limited Critical Reference A program satisfies the limited-critical- reference (LCR) restriction if each statement contains at most one critical reference
3
Consider the first occurrence of n in n n+1. It is assigned to in process p and has (two) occurrences in process q, so it is critical by (a). The second occurrence of n in n n+1 is critical by (b) because it appears in the expression n n+1 in p and is also assigned to in q. Consider now the version of the statements that uses local variables. Again, the occurrences of n are critical, but the occurrences of temp are not. Therefore, the program satisfies the LCR restriction.
4
LCR program
5
LCR Concurrent programs that satisfy the LCR restriction yield the same set of behaviors whether the statements are considered atomic or are compiled to a machine architecture with atomic load and store.
6
Volatile and Non-Atomic Variables Volatile variables
7
The single statement in process q can be interleaved at any place during the execution of the statements of p. Because of optimization during compilation, the computation in q may not use the most recent value of n. The value of n may be maintained in a register from the assignment in p1 through the computations in p2, p3 and p4, and only actually stored back into n at statement p5. Furthermore, the compiler may re-order p3 and 4 to take advantage of the fact that the value of n+5 needed in p3 is computed in p4.
8
These optimizations have no semantic effect on sequential programs, but they do in concurrent programs that use global variables, so they can cause programs to be incorrect. Specifying a variable as volatile instructs the compiler to load and store the value of the variable at each use, rather than attempt to optimize away these loads and stores.
9
Concurrency may also affect computations with multiword variables. A load or store of a full-word variable (32 bits on most computers) is accomplished atomically, but if you need to load or store longer variables (like higher precision numbers), the operation might be carried out non-atomically. A load from another process might be interleaved between storing the lower half and the upper half of a 64-bit variable.
10
If the processor is not able to ensure atomicity for multiword variables, it can be implemented using a synchronization mechanism such as those to be discussed throughout the book. However, these mechanisms can block processes, which may not be acceptable in a real-time system.
11
Concurrency Programs the normal execution of a program on a computer is not the best way to study concurrency. Later in this section, we discuss the implementation of concurrency, because eventually you will wish to write concurrent programs using the constructs available in real languages, but for studying concurrency there is a better way.
12
Concurrent Counting Algorithm The algorithm simply increments a global variable twenty times, ten times in each of two processes.
13
Java Threads CS 556 – Distributed Systems Tutorial on Java Threads
14
Threads A thread is a lightweight process – a single sequential flow of execution within a program Threads make possible the implementation of programs that seem to perform multiple tasks at the same time (e.g. multi-threaded Web servers) A new way to think about programming
15
Java Threads We will cover: How to create threads in Java
16
Java Threads How to create Java Threads There are two ways to create a Java thread: 1.Extend the java.lang.Thread class 2.Implement the java.lang.Runnable interface
17
Java Threads Extending the Thread class In order to create a new thread we may subclass java.lang.Thread and customize what the thread does by overriding its empty run method. The run method is where the action of the thread takes place. The execution of a thread starts by calling the start method.
18
Java Threads Example I class MyThread extends Thread { private String name, msg; public MyThread(String name, String msg) { this.name = name; this.msg = msg; } public void run() { System.out.println(name + " starts its execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + msg); try { Thread.sleep(5000); } catch (InterruptedException ie) {} } System.out.println(name + " finished execution"); }
19
Java Threads Example I class MyThread extends Thread { private String name, msg; public MyThread(String name, String msg) { this.name = name; this.msg = msg; } public void run() { System.out.println(name + " starts its execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + msg); try { Thread.sleep(5000); } catch (InterruptedException ie) {} } System.out.println(name + " finished execution"); }
20
Java Threads Example I class MyThread extends Thread { private String name, msg; public MyThread(String name, String msg) { this.name = name; this.msg = msg; } public void run() { System.out.println(name + " starts its execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + msg); try { Thread.sleep(5000); } catch (InterruptedException ie) {} } System.out.println(name + " finished execution"); }
21
Java Threads Example I (cont.) public class test { public static void main(String[] args) { MyThread mt1 = new MyThread("thread1", "ping"); MyThread mt2 = new MyThread("thread2", "pong"); mt1.start(); mt2.start(); } the threads will run in parallel
22
Java Threads Example I (cont.) Typical output of the previous example: thread1 starts its execution thread1 says: ping thread2 starts its execution thread2 says: pong thread1 says: ping thread2 says: pong thread1 says: ping thread2 says: pong thread1 says: ping thread2 says: pong thread1 says: ping thread2 says: pong thread1 finished execution thread2 finished execution
23
Java Threads Implementing the Runnable interface In order to create a new thread we may also provide a class that implements the java.lang.Runnable interface Preffered way in case our class has to subclass some other class A Runnable object can be wrapped up into a Thread object – Thread(Runnable target) – Thread(Runnable target, String name) The thread’s logic is included inside the run method of the runnable object
24
Java Threads Example II class MyClass implements Runnable { private String name; private A sharedObj; public MyClass(String name, A sharedObj) { this.name = name; this.sharedObj = sharedObj; } public void run() { System.out.println(name + " starts execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + sharedObj.getValue()); try { Thread.sleep(5000); } catch (InterruptedException ie) {} } System.out.println(name + " finished execution"); }
25
Java Threads Example II (cont.) class A { private String value; public A(String value) { this.value = value; } public String getValue() { return value; } public class test2 { public static void main(String[] args) { A sharedObj = new A("some value"); Thread mt1 = new Thread(new MyClass("thread1", sharedObj)); Thread mt2 = new Thread(new MyClass("thread2", sharedObj)); mt1.start(); mt2.start(); } shared variable
26
Java Threads Example II (cont.) Typical output of the previous example: thread1 starts execution thread1 says: some value thread2 starts execution thread2 says: some value thread1 says: some value thread2 says: some value thread1 says: some value thread2 says: some value thread1 says: some value thread2 says: some value thread1 says: some value thread2 says: some value thread1 finished execution thread2 finished execution
27
start() && join() Java Concurrency is built in to the language. – p.start() puts thread p in the ready (Enabled) queue. – p.join(), executed by main, suspends main until thread p terminates.
28
throws If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception. You do this by including a throws clause in the method’s declaration.
29
The throw statement throw expression ; The type of expression must be a subtype of class Throwable. The enclosing block statement terminates abruptly. The thrown exception may be caught by a try-catch statement.
30
Class hierarchy (partial) Throwable Error – OutOfMemoryError – Exception – IOException – RuntimeException – ArithmeticException – IndexOutOfBoundsException – ArrayIndexOutOfBoundsException – StringIndexOutOfBoundsException – NegativeArraySizeException
31
The try-catch-finally statement try body catch(E1 x1) catchBody1 catch(E2 x2) catchBody2... finally finallyBody
32
try-catch with no finally
33
Concurrent Counting Algorithm
34
import java.lang.String; import java.lang.Thread; class Count extends Thread { //field static volatile int n; //constructor void Count() { } //method public void run() { } public static void main(String[] s0) { }
35
class Count extends Thread { static volatile int n = 0; public void run() { int temp; for (int i = 0; i < 10; i++) { temp = n; n = temp + 1; } public static void main(String[] args) { Count p = new Count(); Count q = new Count(); p.start(); q.start(); try { p.join(); q.join(); } catch (InterruptedException e) { } System.out.println("The value of n is " + n); }
36
Java Threads
37
Synchronization of Threads In many cases concurrently running threads share data and must consider the state and activities of other threads. If two threads can both execute a method that modifies the state of an object then the method should be declared to be synchronized, allowing only one thread to execute the method at a time. If a class has at least one synchronized methods, each instance of it has a monitor. A monitor is an object that can block threads and notify them when it is available.
38
Java Threads Synchronization of Threads (cont.) Example: public synchronized void updateRecord() { // critical code goes here … } Only one thread may be inside the body of this function. A second call will be blocked until the first call returns or wait() is called inside the synchronized method.
39
Java Threads Synchronization of Threads (cont.) If you don’t need to protect an entire method, you can synchronize on an object: public void foo() { //… synchronized (this) { //critical code goes here … } //… } Declaring a method as synchronized is equivalent to synchronizing on this for all the method block.
40
Java Threads Putting it all together: a multithreaded server class ConnectionHandler extends Thread{ private Socket s; public ConnectionHandler(Socket s){ this.s = s; start(); } public void run(){ try{ InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); /* handle the socket as explained previously */ … }catch(IOException ex){ ex.printStackTrace(); }
41
Java Threads Putting it all together: a multithreaded server public class MultiThreadedServer { public static final int PORT = 12345; public static void main(String args[]) throws Exception{ ServerSocket ss = new ServerSocket(PORT); while (true){ try{ new ConnectionHandler( ss.accept() ); }catch(IOException ex){ ex.printStackTrace(); }
42
Java Threads Reference Read the Java Tutorial on Threads: http://java.sun.com/docs/books/tutorial/essential/threads/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.