You are on page 1of 4

ThreadsinJava

Thread?
Threading

CPU Multitasking
Java thread mainthread
Java (start)JVM mainthread methodmain()
thread JVM thread thread
garbagecollection,thread objectfinalization thread

thread lightweightprocess thread stack,program


counterlocalvariable thread process

memory,filehandlers preprocessstate

thread UI
CPU I/O thread

CPU controller

()
Thread

thread 1). thread java.lang.Thread


class thread method
(override) run() methodrun() simultaneously
thread 2). thread interfaceRunnable
methodrun() (interfaceRunnable abstractmethodrun())

6/26/20063:54PM

Page1

ThreadsinJava

thread
thread Java thread
(workingthread) threadobject thread workingthread

threadobject JVM
thread thread(start)

thread

thread methodstart() threadobject


thread
CalculatePrimescalPrimes=newCalculatePrimes();

thread calPrimes classCalculatePrimes extendsThread


thread start()
thread
calPrimes.start();
methodstart() methodrun() ( run())

thread methodrun()

() thread thread
deadstate methodrun() NotRunnable method
sleep() thread methodwait() thread blockingI/O

thread class class


SimpleThread TwoThreadsTest

6/26/20063:54PM

Page2

ThreadsinJava

public class SimpleThread extends Thread {


public SimpleThread(String str) {
super(str);
}
public void run() {
for(int i = 0; i < 10; i++) {
System.out.printf("%d %s%n", i, getName());
try {
sleep((long)(Math.random() * 1000));
}
catch(InterruptedException e) {}
}
System.out.printf("DONE! %s%n", getName());
}
}

Method SimpleThread constructor String argument


constructor superclass Thread thread String
method thread methodrun() thread

run() for/loop 10
thread sleep()

for/loop DONE!
thread
public class TwoThreadsTest {
public static void main(String[] args) {
new SimpleThread("Bangkok").start();
new SimpleThread("Chaing Mai").start();
}
}

thread:TwoThreadsTest methodmain() SimpleThread2


Bangkok ChiangMai methodstart() thread
methodrun() compile run

6/26/20063:54PM

0:Bangkok
0:ChiangMai
1:Bangkok
1:ChiangMai
2:Bangkok
3:Bangkok
2:ChiangMai
3:ChiangMai
4:Bangkok
4:ChiangMai
5:Bangkok

6:Bangkok
5:ChiangMai
6:ChiangMai
7:Bangkok
7:ChiangMai
8:Bangkok
8:ChiangMai
9:ChiangMai
9:Bangkok
DONE!ChiangMai
DONE!Bangkok

Page3

ThreadsinJava

thread thread run


(concurrentexecution) methodrun() thread
for/loop thread (terminate)

thread interfaceRunnable
public class SimpleThread2 implements Runnable {
private String name;
public SimpleThread2(String str) {
name = str;
}
public void run() {
for(int i = 0; i < 10; i++) {
System.out.printf("%d: %s%n", i, this.name);
try {
Thread.sleep((long)(Math.random() * 1000));
}
catch(InterruptedException e) {}
}
System.out.printf("DONE! %s%n", this.name);
}
}

extendsThread implementsRunnable SimpleThread2


classvariable:name thread methodrun()
sleep() Thread.sleep()
public class TwoThreadsTest2 {
public static void main(String[] args) {
Runnable bkk = new SimpleThread2(Bangkok");
Thread bangkok = new Thread(bkk);
bangkok.start();
Runnable cnx = new SimpleThread2(Chiang Mai");
Thread chiangmai = new Thread(cnx);
chiangmai.start();
}
}

thread Runnableobject method


start() thread thread bangkok chiangmai

6/26/20063:54PM

Page4

You might also like