Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java ThreadsGraphics Programming Graphics Programming: Java Threads.

Similar presentations


Presentation on theme: "Java ThreadsGraphics Programming Graphics Programming: Java Threads."— Presentation transcript:

1 Java ThreadsGraphics Programming Graphics Programming: Java Threads

2 Agenda Definitions and the Java Thread Model Creating Threads in Java Suspending Threads (sleeping) Interrupting Threads Synchronising Thread processes

3 The Java Thread Model Software engine that can process instructions Allow for simulated concurrent processing Provides asynchronous input & output Essential for tasks such as animation Thread is not a program

4 New Threads 1 Create a Thread by Extending the Thread Class public class MyThread extends Thread { public void run () { // your instructions here }

5 New Threads 1 Instantiating: MyThread testThread = new MyThread( ); You can use any constructor that you define in your extended class. As in other classes, the default constructor for the superclass (Thread()) will be called automatically

6 New Threads 2 Create a Thread by implementing the Runnable Interface: Runnable has only one method - public void run(); public class MyRunnable implements Runnable { public void run(){ // your instructions here }

7 New Threads 2 Instantiating: MyRunnable firstRunnable = new MyRunnable ()

8 New Threads 2 A class that implements the Runnable interface can: extend any other class. may itself be the target of one or more threads throughout the Runnable threads existence. e.g Thread testThread = new Thread(firstRunnable);

9 Starting Threads Starting an Instance of a Thread: testThread.start()

10 Starting Threads testThread.start() This starts a new flow of execution with your instructions and returns immediately. From that moment onward, the instructions that you specified in your Thread’s run method will be executing in parallel to whatever follows the start() method call.

11 Threads in the Running State Execute their methods concurrently with other Threads Co-operate and share resources compete to get their tasks done as soon as possible On a single CPU, time slicing between Threads gives the illusion of true concurrency Threads runtime data stored in private memory space Different Threads can act on the same object

12 Sleeping Threads zzzzz…. Call the Thread’s sleep method Thread.sleep(5*60*1000) // for a five-minute nap This makes the current thread sleep for five minutes, while other threads keep running.

13 Interrupting Threads sleepingThread.interrupt( ) ; flag gets set inside the thread to wake it up throws an exception Thus the sleep call needs to be surrounded inside a try / catch block

14 Thread Exceptions try { Thread.sleep(sleepingTime) ; } catch(InterruptedExeption e) { // do something about it }

15 Threads: Synchronizing Access Synchronizing methods protect the current thread’s target object’s state from being modified by another thread. The current thread has a lock on the object preventing other Threads from accessing the object. Similar to a file being ‘locked to write’ public synchronized void aMethod() { // process data in local variables etc. }


Download ppt "Java ThreadsGraphics Programming Graphics Programming: Java Threads."

Similar presentations


Ads by Google