Professional Documents
Culture Documents
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
//
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()
}
}
10-1 OnlyThread.java
10-2 ThreadDemo1.java
10.1.3 Thread
}
}
2 new
ThreadType tt = new ThreadType();
3 start()
tt.start();
4 run()
void run();
y 160 y
10
// 10.3
ThreadDemo2.java
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
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
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
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
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
//
//
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
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()
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
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
Java CPU
CPU
Thread.MAX_PRIORITY
Thread.MIN_PRIORITY Thread.NORM_PRIORITY
public final int getPriority()
y 170 y
10
//
10.9
ThreadPriority.java
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.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
10.4.2
synchornized
synchonized
class {
public synchonized (){
......
}
}
synchornized
synchornized(obj)
{
//.
}
obj
obj
public void method()
{
Object obj= new Object();
synchornized(obj)
{
//..
}
}
// Object obj
//
y 174 y
10
class method
{
Object o = new Object(); // Object o
public void test()
{
synchornized(o)
//
{
//
}
}
}
synchornized(o)
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
}
}
10-13
y 176 y
ThreadSynchronizedMain.java
10
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
}
10-14 ThreadCommunication.java
10.12
JVM
notifiAll()
wait wait
10.6
synchronized
//
10.13
ThreadLocked.java
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
}
}
}
Java
10-15 ThreadLocked.java
stop()suspend()resume() destroy()
stop()
suspend()/resume ()
suspend()
destroy()
10.7
Java
Java Java
y 182 y