You are on page 1of 26

y 157 y

Java

10

10.1

Java Thread
Runnable

10.1.1

Internet

CPU
Java

Java

ThreadThread of Control

Java
Java

y 158 y

10

10.1.2 Thread
Java
Thread run runnable
Thread
Thread Thread run new
run
main
main

//
10.1 OnlyThread.java

public class OnlyThread{


public static void main(String args[ ]){
run();
// run()
}
// run()
public static void run()
{
for (int count = 1,row = 1; row < 10; row++,count++) //*
{
for (int i = 0; i < count; i++)
// count *
{
System.out.print('*');
//*
}
System.out.println();
//
}
}
}

javac class java class


10-1
10.1
java.lang.Thread run Thread
Thread
Thread run
Thread Thread Thread
//
10.2
ThreadDemo1.java
class ThreadDemo1 extends Thread{
// ThreadDemo1
ThreadDemo1(){}
// ThreadDemo1
ThreadDemo1(String szName)
{
super(szName);
}
// run
public void run()

//

y 159 y

Java
{
for (int count = 1,row = 1; row < 10; row++,count++) //*
{
for (int i = 0; i < count; i++)
// count *
{
System.out.print('*');
//*
}
System.out.println();
//
}
}
public static void main(String argv[ ]){
ThreadDemo1 td = new ThreadDemo1(); // ThreadDemo1 td
td.start();
// start()
}
}

javac class java class


10-2

10-1 OnlyThread.java

10-2 ThreadDemo1.java

10.2 10.1 10.1 run


10.2 start 10.2 Thread

10.1.3 Thread

1 Thread Thread run()


class ThreadType extends Thread{
public void run(){

}
}

2 new
ThreadType tt = new ThreadType();

3 start()
tt.start();

4 run()
void run();
y 160 y

10

// 10.3
ThreadDemo2.java

class ThreadDemo2 extends Thread{


//
ThreadDemo2(){}
//
ThreadDemo2(String szName)
{
super(szName);
//
}
// run
public void run()
{
for (int count = 1,row = 1; row < 10; row++,count++) //*
{
for (int i = 0; i < count; i++)
// count *
{
System.out.print('*');
//*
}
System.out.println();
//*
}
}
public static void main(String argv[ ]){
ThreadDemo2 td1 = new ThreadDemo2(); // ThreadDemo2 td1
ThreadDemo2 td2 = new ThreadDemo2(); // ThreadDemo2 td2
ThreadDemo2 td3 = new ThreadDemo2(); // ThreadDemo2 td3
td1.start();
// td1
td2.start();
// td2
td3.start();
// td3
}
}

10-3 ThreadDemo2.java

javac
class
java class
10-3
10.3 3 td1td2td3
run

Java

y 161 y

Java

10.1.4 Runnable
Runnable Runnable
Java Runnable run()
Runnable
run() Runnable run

//
10.4
ThreadDemo3.java

class ThreadDemo3 implements Runnable{


// run
public void run()
{
for (int count = 1,row = 1; row < 10; row++,count++) //*
{
for (int i = 0; i < count; i++)
// count *
{
System.out.print('*');
//*
}
System.out.println();
//
}
}
public static void main(String argv[ ]){
Runnable rb = new ThreadDemo3();
// ThreadDemo3 rb
Thread td = new Thread(rb);
// Thread
td.start();
// td
}
}

javac
class java class
10-4
10.4 10.2
Runnable

10.1.5 Runnable
10-4 ThreadDemo3.java

Runnable
1 Runnable run
class ThreadType implements Runnable{
public void run(){

}
}

2 new ThreadType
Runnable rb = new ThreadType ();
y 162 y

10

3 Runnable
new Thread(ThreadType) ThreadType run() run()
Thread td = new Thread(rb);

4 ThreadType start()
td.start();

Runnable
//
10.5
ThreadDemo4.java

class ThreadDemo4 implements Runnable{


// run
public void run()
{
for (int count = 1,row = 1; row < 10; row++,count++) //*
{
for (int i = 0; i < count; i++)
// count *
{
System.out.print('*');
//*
}
System.out.println();
//
}
}
public static void main(String argv[ ]){
Runnable rb1 = new ThreadDemo4();
// ThreadDemo4 rb1
Runnable rb2 = new ThreadDemo4();
// ThreadDemo4 rb2
Runnable rb3 = new ThreadDemo4();
// ThreadDemo4 rb3
Thread td1 = new Thread(rb1);
// td1
Thread td2 = new Thread(rb2);
// td2
Thread td3 = new Thread(rb3);
// td3
td1.start();
// td1
td2.start();
// td2
td3.start();
// td3
}
}

javac class
java class
10-5
10.5 10.3 3 td1
td2td3 10.3

10.2

10-5 ThreadDemo4.java
y 163 y

Java

10.2.1

4 4

new new start


start
start stop

runnable start()

Runnable
non Runnable
wait()
notify notifyAll
done stop

run
10-6
10-6 new

start

start

stop

stop

resumenotify
10-6
10-1

y 164 y

10
10-1

start()

New

Runnable

stop()

New Runnable

Done

sleep(long)

Runnable

NonRunnable

sleep(long,int)

Runnable

NonRunnable

suspend()

Runnable

NonRunnable

resume()

NonRunnable

Runnable

yield()

Runnable

Runnable

wait()

Runnable

NonRunnable

notify()

NonRunnable

Runnable

stop()suspend() resume()
suspend() resume() wait() sleep()
stop()

10.2.2

Java Java
Thread Java
start
run() Thread run()
Thread
run()Java
run()

Thread run()Thread
run()
run()
Runnable run() Runnable()
run() Thread

//
10.6
ThreadStart.java

public class ThreadStart


{
public static void main(String[ ] args)
{
Runnable r = new RunnableThread();
// RunnableThread
Thread rt = new Thread(r);
// rt
rt.start();
//
SubThread st = new SubThread("SubThread");// SubThread
y 165 y

Java
st.start();

//

}
}
class RunnableThread implements Runnable{
// run
public void run()
{
System.out.println("RunnableThread ");
}
}
class SubThread extends Thread{
SubThread(){}
// SubThread
SubThread(String Name)
{
super(Name);
}
// run
public void run()
{
System.out.println("SubThread ");
}
}

//

// SubThread

//

//

javac class java class


10-7
10.6 RunnableThread Runnable
run SubThread Thread
run main RunnableThread
rt SubThread
st
10-7 ThreadStart.java
run()
start() start()
run()
start()
run()start()start() run()

10.2.3

1 notify() notifyAll()
public final void notify()
public final void notifyAll()

notify notifyAll
wait notify Java
y 166 y

10

2 sleep(millis)millis
static void sleep(long millis) throws InterruptedException

millis
sleep
static void sleep(long millis, int nanos) throws InterruptedException

millisnanos

3 I/O
2

1 wait()
public final void wait() throws InterruptedException

notify() notifyAl()

2 sleep()
3 I/O

//
10.7
ThreadSleep.java

public class ThreadSleep


{
public static void main(String[ ] args)
{
SubThread st = new SubThread("SubThread");
// SubThread st
st.start();
// st
}
}
class SubThread extends Thread{
SubThread(){}
// SubThread
// SubThread
SubThread(String Name)
{
super(Name);
//
}
// run
public void run()
{
for (int count = 1,row = 1; row < 10; row++,count++) //*
{
for (int i = 0; i < count; i++)
// count *
{
System.out.print('*');
//*
}
try
//try-catch
{
y 167 y

Java
Thread.sleep(1000);
System.out.print("\t wait........");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println();

// 1

// InterruptedException
//
//

}
}
}

javac
class java class
10-8
10.7 * 1
sleep() 1
st sleep
InterruptedException

10-8

10.2.4

ThreadSleep.java

isAlive()
isAlive() true falseisAlive()
trueisAlive()

public final boolean isAlive()

start
10.3

while/isAlive/sleep
1.start();
while( 1.isAlive())
{
Thread.sleep();
}
2.start();

join()
public final void join(long millis) throws InterruptedException

millis 0
y 168 y

10
public final void join(long millis,int nanos) throws InterruptedException

millisnanos
public final void join() throws InterruptedException

//
10.8 WaitThreadStop.java

class WaitThreadStop extends Thread{


// WaitThreadStop
WaitThreadStop(){}
//
WaitThreadStop(String szName)
{
super(szName);
//
}
// run
public void run()
{
for (int count = 1,row = 1; row < 10; row++,count++)
{
for (int i = 0; i < count; i++)
{
System.out.print('*');
//*
}
System.out.println();
//
}
}
}
public class WaitThreadStopMain{
public static void main(String argv[ ]){
WaitThreadStopMain test = new WaitThreadStopMain();
//
WaitThreadStopMain test
test.Method1();
// Method1
//test.Method2();
}
//while/isAlive/sleep
public void Method1(){
WaitThreadStop th1 = new WaitThreadStop(); // WaitThreadStop th1
WaitThreadStop th2 = new WaitThreadStop(); // WaitThreadStop th2
//
th1.start();
//
while(th1.isAlive()){
try{
Thread.sleep(100);
// 100
}catch(InterruptedException e)
{
e.printStackTrace();
//
}
}
//

th2.start();
// th2
}
// join
y 169 y

Java
public void Method2(){
WaitThreadStop th1 = new WaitThreadStop(); // WaitThreadStop th1
WaitThreadStop th2 = new WaitThreadStop(); // WaitThreadStop th2
//
th1.start();
try{
th1.join();
//th1 join
}catch(InterruptedException e)
{
e.printStackTrace();
//
}
//

th2.start();
}
}

javac
class
java class
10-9
10.8 th1
th2 th1
isAlive

join()

10.3
10-9 WaitThreadStop.java

CPU CPU CPU

Java CPU

CPU

public final void setPriority(int newPriority)

newPriority newPriority MIN_PRIORITY


MAX_PRIORITY 1 10 Windows 3

Thread.MAX_PRIORITY
Thread.MIN_PRIORITY Thread.NORM_PRIORITY
public final int getPriority()

y 170 y

10

//
10.9
ThreadPriority.java

class InheritThread extends Thread {


// run()
public void run(){
System.out.println("InheritThread is running");
//
for(int i=0;i<10;i++){
System.out.println(" InheritThread: i="+i);
//
try{
Thread.sleep((int)Math.random()*1000); //
}
catch(InterruptedException e)
//
{}
}
}
}
// Runnable
class RunnableThread implements Runnable{
// run()
public void run(){
System.out.println("RunnableThread is running");
//
for(int i=0;i<10;i++){
System.out.println("RunnableThread : i="+i);
// i
try{
Thread.sleep((int)Math.random()*1000);
//
}
catch(InterruptedException e){
//
}
}
}
}
public class ThreadPriority{
public static void main(String args[ ]){
// Thread
InheritThread itd=new InheritThread();
// Runnable
Thread rtd=new Thread(new RunnableThread());
itd.setPriority(5);
// myThread1 5
rtd.setPriority(5);
// myThread2 5
itd.start();
// itd
rtd.start();
// rtd
}
}

javac class java class


10-10
10.9 rtd itd CPU

itd.setPriority(1);
rtd.setPriority(10);

// myThread1 1
// myThread2 10

10-11
y 171 y

Java

10-10 ThreadPriority.java

10-11 ThreadPriority.java

10.9 itd rtd rtd


rtd CPU

10.4
Java
Java

10.4.1

Thread1
Thread2
Thread1 T
Thread2 T
Thread2
T Thread1 T

//
10.10
ThreadNoSynchronized.java

class ShareData{
public static String szData = "";
//
y 172 y

10
}
class ThreadDemo extends Thread{
private ShareData oShare;
// ShareData
ThreadDemo(){}
// ThreadDemo
// ThreadDemo
ThreadDemo(String szName,ShareData oShare){
super(szName);
//
this.oShare = oShare;
// oShare
}
public void run(){
for (int i = 0; i < 5; i++){
if (this.getName().equals("Thread1")){
oShare.szData = " 1 ";
//
try{
Thread.sleep((int)Math.random() * 100);
//
}
catch(InterruptedException e){
//
}
System.out.println(this.getName() + ":" + oShare.szData); //
}else if(this.getName().equals("Thread2"))
{
oShare.szData = " 2 ";
//
try{
Thread.sleep((int)Math.random() * 100); //
}catch(InterruptedException e)
//
{}
System.out.println(this.getName() + ":" + oShare.szData); //
}
}
}
}
public class ThreadNoSynchronized {
public static void main(String argv[ ]){
ShareData oShare = new ShareData();
// ShareData oShare
ThreadDemo th1 = new ThreadDemo("Thread1",oShare);
// th1
ThreadDemo th2 = new ThreadDemo("Thread2",oShare);
// th2
th1.start();
// th1
th2.start();
// th2
}
}

javac
class
java class
10-12
10.10 Thead1
1 Thead2 2

Java
10-12 ThreadNoSynchronized.java
y 173 y

Java

Java
synchronized
synchonized Java

wait

Java wait()notify() notifyAll()


synchonized
wait()
notify() notifyAll()

10.4.2

synchornized
synchonized
class {
public synchonized (){
......
}
}

synchornized
synchornized(obj)
{
//.
}

obj
obj
public void method()
{
Object obj= new Object();
synchornized(obj)
{
//..
}
}

// Object obj
//

obj Object obj = new Object()


obj obj

y 174 y

10
class method
{
Object o = new Object(); // Object o
public void test()
{
synchornized(o)
//
{
//
}
}
}

synchornized(o)

public void method()


{
synchornized(this)
//
{
//
}
}

synchornized(this)
synchornized(this)
synchornized(this)
synchornized(this)
ClassName
synchornized(ClassName.class)
{
//.
}

10.4.3

synchornized 10.10
oShare
//
10.11
ThreadSynchronizedMain.java
class ShareData{
public static String szData = ""; // szData
}
class ThreadDemo extends Thread{
private ShareData oShare;
// ShareData
ThreadDemo(){}
//
//
ThreadDemo(String szName,ShareData oShare){
super(szName);
//
this.oShare = oShare;
// oShare
}
public void run(){
y 175 y

Java
// oShare
synchronized (oShare){ // oShare
for (int i = 0; i < 5; i++){
//
if (this.getName().equals("Thread1"))
// Thread1
{
oShare.szData = " 1 ";
//
try{
Thread.sleep((int)Math.random() * 50);
//
}
catch(InterruptedException e){
//
}
System.out.println(this.getName() + ":" + oShare.szData); //
}else if(this.getName().equals("Thread2"))
// Thread2
{
oShare.szData = " 2 ";
//
try{
Thread.sleep((int)Math.random() * 50);
//
}catch(InterruptedException e)
//
{}
System.out.println(this.getName() + ":" + oShare.szData); //
}
}
}
}
}
public class ThreadSynchronizedMain {
public static void main(String argv[ ]){
ShareData oShare = new ShareData();
// ShareData oShare
ThreadDemo th1 = new ThreadDemo("Thread1",oShare); // th1
ThreadDemo th2 = new ThreadDemo("Thread2",oShare); // th2
th1.start();
// th1
th2.start();
// th2
}
}

javac class java class


10-13

10-13

y 176 y

ThreadSynchronizedMain.java

10

10.11 th1 th2


run th1 th1 th1
th2

synchronized synchronized

10.5
Java
Object wait()notify()notifyAll()Java

10.5.1

/wait/notify) wait
notifyAll() notify() 10.12

// 10.12 Producer.java

class Producer extends Thread
//
{
Queue q;
// q
Producer(Queue q)
{
this.q = q;
}
public void run()
{
for(int i=1;i<5;i++)
{
q.put(i);

//
// q

//
//
y 177 y

Java
}
}
}
class Consumer extends Thread
{
Queue q;
Consumer(Queue q)
{
this.q = q;
}
public void run()
{
while(true)
{
q.get();
}
}

// q
//
// q

//
//

Producer
run Consumer
run

10.5.2

value
isEmpty put get
class Queue
{
int value = 0;
// value
boolean isEmpty = true;
// isEmpty
//
public synchronized void put(int v)
{
//
if (!isEmpty)
{
try
{
System.out.println("");
wait();
//
}
catch(Exception e)
//
{
e.printStackTrace();
//
}
}
value += v;
//value v
isEmpty = false;
//isEmpty false
y 178 y

10
System.out.println(""+v);
//
notify();
//
}
//
public synchronized int get()
{
//
if (isEmpty)
{
try
{
System.out.println("");
//
wait();
//
}
catch(Exception e)
//
{
e.printStackTrace();
//
}
}
value--;
//value -1
if (value < 1)
{
isEmpty = true;
//isEmpty true
}
System.out.println(","+value);
//
notify();
//,
return value;
// value
}
}

put
notify() get

notify()

10.5.3

/
start
//

ThreadCommunication.java
public class ThreadCommunication
{
public static void main(String[ ] args)
{
Queue q = new Queue();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
c.start();
p.start();

//
//
//
//
//

}
y 179 y

Java
}

javac class java class


10-14

10-14 ThreadCommunication.java

10.12

JVM
notifiAll()
wait wait

10.6
synchronized

//
10.13
ThreadLocked.java

public class ThreadLocked implements Runnable {


public static boolean flag = true;
//
private static Object A = new Object();
// Object A
y 180 y

10
private static Object B = new Object();
// Object B
public static void main(String[ ] args) throws InterruptedException {
Runnable r1 = new ThreadLocked();
// ThreadLocked r1
Thread t1 = new Thread(r1);
// t1
Runnable r2 = new ThreadLocked();
// ThreadLocked r2
Thread t2 = new Thread(r2);
// t2
t1.start(); // t1
t2.start(); // t2
}
public void AccessA()
{
flag = false;
// flag
//
synchronized (A) {
// A
System.out.println(" t1 : A ");
//
try {
//, B
Thread.sleep(1000);
//
} catch (InterruptedException e) {
//
e.printStackTrace();
//
}
System.out.println(" t1 : B ");
// A , B
//
synchronized (B) {
// B
System.out.println(" t1 : B ");
//
}
}
}
public void AccessB()
{
flag = true;
// flag
//
synchronized (B) {
// B
System.out.println(" t2 : B ");
//
try {
//, A
Thread.sleep(1000);
//
} catch (InterruptedException e) {
// InterruptedException
e.printStackTrace();
//
}
System.out.println(" t2 : A ");
//
// B , A
//
synchronized (A) {
// A
System.out.println(" t2 : A ");
//
}
}
}
public void run() {
if (flag)
// flag true
{
AccessA();
// AccessA
}else
y 181 y

Java
{
AccessB();

// AccessB

}
}
}

javac class java class


10-15
10.13 t1 t2
AccessA AccessB
t1 A B t2
B A

Java
10-15 ThreadLocked.java

stop()suspend()resume() destroy()
stop()
suspend()/resume ()
suspend()
destroy()

10.7
Java
Java Java

y 182 y

You might also like