Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.

Similar presentations


Presentation on theme: "Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates."— Presentation transcript:

1 Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates

2 What is a Thread Threads are smaller portions of a bigger program that appear to run in parallel. Car Engine Analogy At least one thread is present in every java application. Threads have only a certain amount of time to complete their operations this is called a time slice.

3 More on slicing Every thread as a priority. The priority of a thread is the same as the priority of the thread that created unless it is changed. Threads take turns working for a very small amount of time and then letting the thread with the next highest priority go next. Factory Line Analogy

4 Problems that with time slices Race Condition- if a thread is modifying a variable shared by multiple threads and the order of operations matters sometimes the thread will not finish the first operation before it goes onto the next. This is fixed by locking and unlocking object, which will be demonstrated in the code example.

5 Locking and Unlocking A locked variable cannot be modified by any thread except for the thread that locked it Even if the threads time slice ends during its operation, other threads will be unable to use the locked variables. The locking thread will be able to complete it’s operation. A thread that is finished with its operation it should unlock all of its variables

6 Dead Locking Locking and Unlocking does not fix all of the problems with threads. If you create a situation where all of your threads are waiting for another thread to do something you have created a Dead Lock. Dead Locks can be solved by forcing a method to await a condition. After the condition has been met thread that meets the condition will notify all of the threads to continue out of dead lock

7 Creating a Thread All threads must either implement the Runnable interface or extend a class that implements the Runnable interface. The only method that is in the Runnable interface is the run() method that returns void. Methods that are generally used in threads are sleep() and die().

8 Methods in Threads Run() tells the thread what to do when it is called Sleep(long milliseconds) tells the thread how to wait before it tries to run again. Die() tells the thread that it needs to die and makes it release any objects that it has locked.

9 Tying it all together Threads are a small component of a greater program, that appear to be running in parallel. Exceptions: InterruptedException, SecurityException,


Download ppt "Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates."

Similar presentations


Ads by Google