My other sites |
New Summer Look :)
![]() |
- An integral part of Java - From day one - Shared address space and memory |
![]() |
Two ways to create a Thread: 1. Implement Runnable & implement its run() method 2. Extend Thread & override its run() method This is due to single inheritance in Java Extending Thread means you cannot extend another class The first alternative is more used |
![]() |
Runnable is a very simple interface, it has just one
method The Thread's start() method calls the Runnable's run() method public interface Runnable { public abstract void run(); } |
There are four basic constructors; their arguments can
be: 1. A name (String) for the Thread (default null) - Should be unique, used for debugging etc. 2. An object (Runnable) to call a run() method in - Default is this object class ThreadExample implements Runnable { public ThreadExample { Thread t1 = new Thread(); Thread t2 = new Thread("sun"); Thread t3 = new Thread(this); Thread t4 = new Thread(this, "t4"); String n2 = getName(); t3.setName("t3"); } public void run() { } } |
run() start() stop() suspend() resume() destroy() isAlive() yield() join() sleep(long) interrupt() |
Run it! (Runnable) Activates the thread and calls run() Forces the thread to stop Temporarily halt the thread Resume a halted thread Equivalent to UNIX's "kill -9" Is it running? Let another thread run Wait for the death of a thread Sleep for a number of milliseconds Interrupt sleep, wake up! |
![]() |
Inter-thread communication methods are declared in
java.lang.Object Each object can be associated with a monitor (a sort of thread lock) wait() - Suspend the thread - Wait can also be time limited notify() - Unlock the first monitored thread - (The first that called wait() within the monitor) notifyAll() - Unlocks all monitored threads - Highest prioritised first |
![]() |
class MyThread implements Runnable { MyThread() { } public void run() { // Loop forever...? } public static void main(String[] args) { Thread mythread = new Thread(new MyThread()); mythread.start(); mythread.stop(); } } |
class MyThread extends Thread { MyThread() { } public void run() { // Loop forever... } public static void main(String args[]) { MyThread myThread = new MyThread(); myThread.start(); myThread.stop(); } } |
![]() |
- Priority per thread
public class Thread implements Runnable { ... public final static int MIN_PRIORITY; // 1 public final static int NORM_PRIORITY; // 5 public final static int MAX_PRIORITY; // 10 public final int getPriority(); public final void setPriority(int); ... } |
![]() |
currentThread()
|
All objects may be associated with a monitor Monitors aggregate mutually exclusive locks - Synchronized methods - Synchronized statements |
A synchronized method -> mutual exclusion (lock) per object
|
![]() |
This is an example of synchronized methods:public class Account { private double balance; public Acount(double initialDeposit) { balance = initialDeposit; } public synchronized double getBalance() { return balance; } public synchronized void deposit(double amount) { balance += amount; } } |
![]() |
The synchronized statement: - mutual exclusion (lock) of data access instead of a synchronized method - very lightweight Syntax: synchronized(Object) { } |
![]() |
Example (mutual exclusive lock on the variable values)
:public static void abs(int[] values) { synchronized(values) { for (int i=0; i < values.length; i++) { if (values[i] < 0) values[i] = -values[i]; } } } |
![]() |
- A thread group represents a set of threads - In addition, a thread group can also include other thread groups - A thread may only access information about its own thread group public Thread(ThreadGroup, Runnable); public Thread(ThreadGroup, Runnable, String); public Thread(ThreadGroup, String); - The VM is one ThreadGroup while the application is another one |
![]() |
![]() |
import java.awt.*; import java.awt.event.*; public class ThreadRace extends Frame { public static void main(String[] args) { ThreadRace myThreadRace = new ThreadRace( |
![]() |
1. Imagine that one thread, T1, needs to access two objects
(one file for writing banking data, and a String that contains the
balance). These two objects are declared Synchronized, so that no
other thread can access these objects at the same time. A second
thread, T2, runs in parallel with T1 and needs to access the same
objects too. While T1 locks the file (but not the String), T2 manages
to lock the String, but not the file. What happens next? |
![]() |
1. Create a console application and create and use two threads that are printing an increasing counter. Change the threads priorities and notice the effect on the counters. Use the Threads sleep method if you need to slow down the printing 2. Create a simple animation on a Container which holds a Pause and a Play button. To do so you will need to control a thread that loops and calls Panels repaint method 3. Create a Frame and put three of the above containers. Append a list for the user to select from a range of thread priorities. Can you see any difference on your animation? |