Java Threads The Thread Class Synchronized Mutual Exclusion

  • Slides: 10
Download presentation
Java Threads: - The Thread Class - Synchronized (Mutual Exclusion)

Java Threads: - The Thread Class - Synchronized (Mutual Exclusion)

1 -The Thread Class in Java public class Thread extends Object implements Runnable {

1 -The Thread Class in Java public class Thread extends Object implements Runnable { // Constructors public Thread(); // Methods public void start(); public void run(); // Causes this thread to begin execution, start calls run. // This method should be redefined by you. public static native void sleep(long millis) throws Interrupted. Exception; . . } class Thread. Example extends Thread { // This method is called when the thread runs public void run() {. . . your code. . . } public interface Runnable { public abstract void run(); }. . . // Create and start the thread } Thread thread = new Thread. Example(); thread. start(); . . . 2

2 - The Thread Class in Java Thread MAX_PRIORITY = 10 MIN_PRIORITY = 1

2 - The Thread Class in Java Thread MAX_PRIORITY = 10 MIN_PRIORITY = 1 NORM_PRIORITY = 5 Thread() Thread(Runnable) Thread(String name) get. Name() : String set. Name(name: String) get. Priority() : int set. Priority(new. Priority : int) void join(long millis) public class Runable. Example implements Runnable { . . . Thread runner = new Thread(this); runner. start(); . . . } Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Waits for the thread to die. The thread doing the join-call will wait for the other thread to die (the one with the join-method being executed). . Thread t = new Thread. Example(); t. start(); // wait for the thread to complete t. join(); . . . 3

3 - The Thread Class in Java Thread. . . <<static>> current. Thread() :

3 - The Thread Class in Java Thread. . . <<static>> current. Thread() : Thread <<static>> sleep(millis : long) <<static>> yield() is. Alive() : boolean destroy() is. Daemon() : boolean set. Daemon(on : boolean) Returns a reference to the currently executing thread object. Enter the READY-state and invoke the scheduler. Destroys this thread, without any cleanup. A thread might be a daemon thread or a user thread (default). Daemon threads are there to serve user threads; if there are only daemon threads the application will be terminated. Example: the garbage collector. 4

4 - The Thread Class in Java A thread group represents a set of

4 - The Thread Class in Java A thread group represents a set of threads. A thread group can include other thread groups. The thread groups form a tree in which every thread group, except the initial thread group, has a parent. A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups. Thread. . . Thread(Thread. Group, Runnable). . . get. Thread. Group() : Thread. Group <<static>> active. Count() : int <<static>> enumerate(Thread[] tarray) : int Returns the current number of active threads in this thread's thread group. Copies into the specified array every active thread in this thread's thread group and its subgroups. 5

Mutual exclusion can be achieved by using method modifier synchronized. For an object of

Mutual exclusion can be achieved by using method modifier synchronized. For an object of type Box only public class Box{ private Object box. Contents=null; // use put public synchronized Object get(){ Object contents = box. Contents; box. Contents = null; return contents; } one thread can at the same time execute put or get. public synchronized boolean put( Object contents){ if (box. Contents != null) return false; box. Contents = contents; return true; } If one thread is executing put and another thread calls get; the thread trying to execute get must wait until the first one is finished executing put. } 6

q When a synchronized method is called the thread doing the call is trying

q When a synchronized method is called the thread doing the call is trying to lock (grab the lock (mutex)) the object – “lock the door from inside”. q q If the object is already locked by another thread the thread doing the call must wait until the other thread unlocks the object, that is: finished execution of a synchronized method or doing a wait-call (wait is explained later). All objects waiting for the same lock is put in the same queue (one queue for each lock). An object is unlocked when the synchronized method has been executed. 7

q If a synchronized method has a call to a method that is not

q If a synchronized method has a call to a method that is not synchronized, the lock will not be released. q All synchronized methods of an object are working against the same lock (one lock per object). 8

Minimizing the time a thread keeps a lock is a good thing: waiting is

Minimizing the time a thread keeps a lock is a good thing: waiting is reduced since the object is unlocked for a shorter time (the chance of finding the object unlocked is bigger) and the chance for deadlock is also reduced (less statements in a critical region reduces the change of deadlock). public class Account{ private float balance = 0 ; public void transaction(float amount){ synchronized(this){ balance = balance + amount; }; } Instead of synchronizing a complete method it is possible to synchronize a } block. 9

By using a synchronized statement you can choose which object to synchronize against. N.

By using a synchronized statement you can choose which object to synchronize against. N. B. You can not synchronize against null! public class Account{ private Object lock = new Object() ; private float balance = 0 ; public void transaction(float amount){ synchronized(lock){ balance = balance + amount; }; } } 10