Concurrency: Java Threads
Thread Basics
Example: SimpleThread (Run Applet)
public class SimpleThread extends Thread
{
private int countDown = 50;
private static int
threadCount = 0;
private int threadNumber = ++threadCount;
public SimpleThread () {
System.out.println("Making " + threadNumber);
}
public void run() {
while (true) {
System.out.println("Thread"+threadNumber+countDown);
if (--countDown == 0) return;
}
}
public static void main(String[]
args) {
for(int i = 0; i < 5; i++)
new SimpleThread().start();
System.out.println("All Threads Started");
}
}
Example: Counter4 (Run Applet)
public class
Counter4 extends JApplet {
private JButton startB = new JButton("Start");
private Ticker[] s;
class Ticker extends Thread {
private JToggleButton b = new JToggleButton("Toggle");
private JTextField t = new JTextField(10);
private int count = 0;
private boolean runFlag = true;
public Ticker() {
b.addActionListener(new ToggleL());
JPanel p = new JPanel();
p.add(t);
p.add(b);
getContentPane().add(p);
}
class ToggleL
implements ActionListener {
public void actionPerformed(ActionEvent e) {
runFlag = !runFlag;
}
}
public void run() {
while (true) {
if (runFlag)t.setText(Integer.toString(count++));
try {
sleep(200);
} catch(InterruptedException e) {
System.err.println("Interrupted");
}
}
}
}
class StartL
implements ActionListener {
public void actionPerformed(ActionEvent e) {
startB.setBackground(Color.red);
startB.setEnabled(false);
for (int i = 0; i < s.length; i++)
s[i].start();
}
}
}
public void init()
{
Container cp = getContentPane();
cp.setLayout(new
FlowLayout());
s = new Ticker[4];
for (int i
= 0; i < s.length; i++)
s[i] = new Ticker();
startB.addActionListener(new StartL());
cp.add(startB);
}
public static void main (String[] args) {
Counter4
applet = new Counter4 ();
Console.run (applet, 200, 200);
}
}
You can run this as an application:
% java Counter4
Wait, Notify & Interrupt
Example: Notify() a waiting
thread (Run Applet)
public class Notify extends JApplet {
private JTextField
txt = new JTextField(20);
private JButton
suspendB
= new JButton("Suspend"),
resumeB
= new JButton("Resume");
private Suspendable
suspendableThread = new Suspendable();
class Suspendable extends
Thread {
private int
count = 0;
private boolean
suspendedFlag = false;
public Suspendable() {
start();
}
public synchronized void funSuspend()
{
suspendedFlag
= true;
}
public synchronized void funResume()
{
suspendedFlag
= false;
notify();
}
public void run() {
while (true) {
txt.setText("Count:
" + Integer.toString(count++));
synchronized (this) {
if(suspendedFlag)
{
try{
txt.setText("Suspend Button is ON:
wait()");
wait();
}
catch(InterruptedException e) {
System.err.println("wait
Interrupted "+ e);
}
txt.setText("Resume
Button is ON");
}
}// synch
try{
sleep(2000);
}
catch(InterruptedException
e) {
System.err.println("sleep():
Interrupted " + e);
}
}//true
}//run
}//thread
public void init() {
Container cp = getContentPane();
cp.setLayout(new
FlowLayout());
cp.add(txt);
suspendB.addActionListener(
new ActionListener() {
public
void actionPerformed(ActionEvent e) {
suspendableThread.funSuspend();
suspendB.setEnabled(false);
suspendB.setBackground(Color.red);
resumeB.setEnabled(true);
resumeB.setBackground(Color.green);
}
}
);
cp.add(suspendB);
suspendB.setEnabled(true);
suspendB.setBackground(Color.green);
resumeB.addActionListener(
new ActionListener() {
public
void actionPerformed(ActionEvent e) {
suspendableThread.funResume();
suspendB.setEnabled(true);
suspendB.setBackground(Color.green);
resumeB.setEnabled(false);
resumeB.setBackground(Color.red);
}
}
);
cp.add(resumeB);
resumeB.setEnabled(false);
resumeB.setBackground(Color.red);
}
public static void main(String[] args) {
Console.run(new
Notify(), 300, 100);
}
}//JApplet
Example: Interrupt() a waiting thread (Run Applet)
public class Interrupt extends JApplet {
private JTextField
txt = new JTextField(30);
private JButton
suspendB
= new JButton("Suspend"),
interruptB
= new JButton("Interrupt");
private Suspendable
suspendableThread = new Suspendable();
class Suspendable extends
Thread {
private int
count = 0;
private boolean
suspendedFlag = false;
public Suspendable() {
start();
}
public synchronized void funSuspend()
{
suspendedFlag
= true;
}
public synchronized void funInterrupt()
{
suspendedFlag
= false;
interrupt();
}
public void run() {
while (true) {
txt.setText("Count:
" + Integer.toString(count++));
synchronized (this) {
if(suspendedFlag)
{
try{
txt.setText("Suspend Button is ON:
wait()");
wait();
}
catch(InterruptedException e) {
txt.setText("Interrupt
Button is ON: "+ e);
System.err.println("wait
Interrupted "+ e);
}
}
}// synch
try{
sleep(2000);
}
catch(InterruptedException
e) {
System.err.println("sleep():
Interrupted"+e);
}
}//true
}//run
}//thread
public void init() {
Container cp = getContentPane();
cp.setLayout(new
FlowLayout());
cp.add(txt);
suspendB.addActionListener(
new ActionListener() {
public
void actionPerformed(ActionEvent e) {
suspendableThread.funSuspend();
suspendB.setEnabled(false);
suspendB.setBackground(Color.red);
interruptB.setEnabled(true);
interruptB.setBackground(Color.green);
}
}
);
cp.add(suspendB);
suspendB.setEnabled(true);
suspendB.setBackground(Color.green);
interruptB.addActionListener(
new ActionListener() {
public
void actionPerformed(ActionEvent e) {
suspendableThread.funInterrupt();
suspendB.setEnabled(true);
suspendB.setBackground(Color.green);
interruptB.setEnabled(false);
interruptB.setBackground(Color.red);
}
}
);
cp.add(interruptB);
interruptB.setEnabled(false);
interruptB.setBackground(Color.red);
}
public static void main(String[] args) {
Console.run(new
Interrupt(), 400, 200);
}
}//JApplet
Example: Interrupt()
a sleeping thread (Run Applet)
public
class InterruptSleep extends JApplet
{
private JTextField
txt = new JTextField(30);
private JButton
interruptB
= new JButton("Interrupt Sleep");
private Counting countingThread
= new Counting();
class Counting extends Thread {
private int
count = 0;
public Counting() {
start();
}
public synchronized void funInterrupt() {
System.err.println("Interrupt
Button");
interrupt();
}
public void run() {
while (true) {
txt.setText("Count:"+Integer.toString(count++));
try{sleep(8000); }
catch(InterruptedException
e) {
System.err.println("sleep 8 seconds:Interrupted");
}
}//true
}//run
}//thread
public void init()
{
Container cp = getContentPane();
cp.setLayout(new
FlowLayout());
cp.add(txt);
interruptB.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
countingThread.funInterrupt();
}
});
cp.add(interruptB);
interruptB.setEnabled(true);
interruptB.setBackground(Color.green);
}
public static void main(String[] args) {
Console.run(new
InterruptSleep(), 400, 200);
}
}//JApplet