際際滷

際際滷Share a Scribd company logo
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
仂亠 亳 仗仂仂从亳
仂亠 亳仄亠亠 仂弍于亠仆仆 亠亟 亳仗仂仍仆亠仆亳 亳
仂弍于亠仆仆亶 于亟亠仍亠仆仆亶 亠仄 仆舒弍仂 亠仂于.
JVM 于仗仂仍仆磳 从舒从 仂亟亳仆 仗仂亠.
仂仂从亳 (threads)  束仍亠亞从仂于亠仆亠損 (lightweight)
仗仂亠, 仂仆亳 亳仄亠ム 仂弍于亠仆仆 亠亟 亳仗仂仍仆亠仆亳,
仆仂 亳 仂亰亟舒仆亳亠 亠弍亠 仄亠仆亠 亠仂于. 仂仂从亳 于亠亞亟舒
亠于ム 于仆亳 仗仂亠舒, 舒亰亟亠仍 于亠 亠亞仂 亠.
舒亢亟仂亠 Java-仗亳仍仂亢亠仆亳亠 仂仂亳 从舒从 仄亳仆亳仄仄 亳亰 仂亟仆仂亞仂
仗仂仂从舒 (main thread) 亳 仄仂亢亠 仂亰亟舒于舒 亟亞亳亠 仗仂仂从亳 仗仂
仄亠亠 仆亠仂弍仂亟亳仄仂亳.
1
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
仂亠 亳 仗仂仂从亳
亟亠仆仂亠 仗仂舒仆于仂 弌


PID 1, Thread 1



丶



PID 2, Thread 1
PID 3

PID 3

Thread 2

PID 3 Thread 1

Thread 3


丐



PID 4

PID 4

PID 4



弌

Thread 1

Thread 2

Thread 3



弌




2
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
仍舒于仆亶 仗仂仂从
亠 亟仂亠仆亳亠 仗仂仂从亳 仗仂仂亢亟舒ム 亳亰
main thread. 仍舒于仆亶 仗仂仂从 亟仂仍亢亠仆
弍 仗仂仍亠亟仆亳仄 于 从仂仂仂仄
亰舒从舒仆亳于舒亠 于仗仂仍仆亠仆亳亠. 仂亞亟舒
亞仍舒于仆亶 仗仂仂从 仂舒仆舒于仍亳于舒亠
仗仂亞舒仄仄舒 亰舒于亠舒亠.

3
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌仂亰亟舒仆亳亠 仗仂仂从仂于
仂仂从 于 Java  仂弍亠从 从仍舒舒 Thread.
弌仂亰亟舒 仗仂仂从 仄仂亢仆仂 2-仄 仗仂仂弍舒仄亳:
-

仆舒仍亠亟仂于舒 从仍舒 仂 Thread

-

亠舒仍亳亰仂于舒 亳仆亠亠亶 Runnable

仂仂亶 仗仂仂弍 仗亠亟仗仂亳亠仍仆亠亠, .从.
仆舒仍亠亟仂于舒仆亳亠 仂 Thread 亰舒仗亠舒亠
仆舒仍亳亳亠 亟亞亳 仗亠从仍舒仂于.

4
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌仂亰亟舒仆亳亠 仗仂仂从仂于
public class HelloThread extends Thread
{
public void run()
{
System.out.println("Hello from a thread!");
}
public static void main(String args[ ])
{
(new HelloThread()).start();
}
}

5
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌仂亰亟舒仆亳亠 仗仂仂从仂于
public class HelloRunnable implements Runnable
{
public void run()
{
System.out.println("Hello from a thread!");
}
public static void main(String args[ ])
{
(new Thread(new HelloRunnable())).start();
}
}

6
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
亳仂舒仆仂于从舒 仗仂仂从仂于
public static void main(String args[ ]) throws InterruptedException
{String Info[ ] = { "String 1", "String 2", "String 3", "String 4" };
for (int i = 0; i < Info.length; i++)
{

//Pause for 4 seconds
Thread.sleep(4000);
//Print a message
System.out.println(Info[i]);

}
}
7
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
亠于舒仆亳亠 仗仂仂从仂于
亠仂亟: public void interrupt()
亠舒从亳 仆舒 仗亠于舒仆亳亠:
try

if (Thread.interrupted())

{ Thread.sleep(4000);

{

}
catch (InterruptedException e)

return;
}

{ return;
}
亠于亶 于舒亳舒仆 舒弍仂舒亠 亠仍亳 于 仗仂亠亠 于仗仂仍仆亠仆亳 舒仂
于亰于舒ム 仄亠仂亟 仂仗亠亟亠仍亠仆仆亠 从舒从  throws InterruptedException,
于 仗仂亳于仆仂仄 仍舒亠 仆舒亟仂 亳仗仂仍亰仂于舒 于仂仂亶 仄亠仂亟
8
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌于磶于舒仆亳亠 仗仂仂从仂于
亠仂亟: public final void join()
亰于舒ム亳亶 仗仂仂从 仂舒仆舒于仍亳于舒亠 亳 亢亟亠
亰舒于亠亠仆亳 仗仂仂从舒 t

亳仂亳亠 仗仂仂从仂于
 从仍舒亠 Thread 亠于ム 仄亠仂亟
public final int getPriority()
public final void setPriority(int newPriority)
亳 亳 从仂仆舒仆:
MIN_PRIORITY = 1
MAX_PRIORITY = 10
NORM_PRIORITY = 5
9
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌亳仆仂仆亳亰舒亳

public class ThreadTest implements Runnable
{ private static ThreadTest shared = new ThreadTest();
public void process()
{
for (int i=0; i<3; i++)
{System.out.println(Thread.currentThread().getName()+i);
Thread.yield();
}
}

10
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌亳仆仂仆亳亰舒亳
public void run()
{
shared.process();
}
public static void main(String s[ ])
{
for (int i=0; i<3; i++)
{
new Thread(new ThreadTest(), "Thread-"+i).start();
}
}
}
11
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌亳仆仂仆亳亰舒亳
亳仄亠 于于仂亟舒:
Thread-0 0
Thread-1 0
Thread-2 0
Thread-0 1
Thread-2 1
Thread-0 2
Thread-1 1
Thread-2 2
Thread-1 2

12
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌亳仆仂仆亳亰舒亳
public void run()
{
synchronized (shared)
{
shared.process();
}
}
public static void main(String s[ ])
{
for (int i=0; i<3; i++)
{
new Thread(new ThreadTest(), "Thread-"+i).start();
}
}
}
13
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌亳仆仂仆亳亰舒亳
亳仄亠 于于仂亟舒:
Thread-0 0
Thread-0 1
Thread-0 2
Thread-1 0
Thread-1 1
Thread-1 2
Thread-2 0
Thread-2 1
Thread-2 2

14
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌亳仆仂仆亳亰舒亳
Synchronized-仄亠仂亟 舒弍仂舒ム 舒仆舒仍仂亞亳仆仄 仂弍舒亰仂仄.
亠亢亟亠, 亠仄 仆舒舒 于仗仂仍仆 亳, 仗仂仂从 仗舒亠
亰舒弍仍仂从亳仂于舒 仂弍亠从,  从仂仂仂亞仂 于亰于舒亠 仄亠仂亟.
仂仍亠 于仗仂仍仆亠仆亳 弍仍仂从亳仂于从舒 仆亳仄舒亠.
public class ThreadTest implements Runnable
{ private static ThreadTest shared = new ThreadTest();
public void synchronized process()
{
for (int i=0; i<3; i++)
{System.out.println(Thread.currentThread().getName()+i);
Thread.yield();
}
}
}
15
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
弌亳仆仂仆亳亰舒亳
丐舒从亢亠 亟仂仗亳仄 仄亠仂亟 static synchronized. 亳
亳 于亰仂于亠 弍仍仂从亳仂于从舒 舒仆舒于仍亳于舒亠 仆舒 仂弍亠从
从仍舒舒 Class, 仂于亠舒ム亠亞仂 亰舒 亳仗,  从仂仂仂亞仂
于亰于舒亠 仂 仄亠仂亟.

16
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
丐仗亳从亳 (deadlocks)
public class DeadlockDemo
{public final static Object one=new Object(), two=new Object();
public static void main(String s[ ]) {
Thread t1 = new Thread()
{ public void run()
{synchronized(one)
{Thread.yield();
synchronized (two) {System.out.println("Success!"); }
}
}
};

17
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
丐仗亳从亳 (deadlocks)
Thread t2 = new Thread()
{public void run()
{synchronized(two)
{Thread.yield();
synchronized (one) {System.out.println("Success!"); }
}
}
};
t1.start(); t2.start();
}
}

18
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
Wait-set 仄亠仂亟
舒亢亟亶 仂弍亠从 于 Java 亳仄亠亠 仆亠 仂仍从仂 弍仍仂从亳仂于从 亟仍
synchronized 弍仍仂从仂于 亳 仄亠仂亟仂于, 仆仂 亳 舒从 仆舒亰于舒亠仄亶 waitset, 仆舒弍仂 仗仂仂从仂于 亳仗仂仍仆亠仆亳. ミ頴笑 仗仂仂从 仄仂亢亠 于亰于舒
仄亠仂亟 wait() 仍ミ頴笑覚 仂弍亠从舒 亳 舒从亳仄 仂弍舒亰仂仄 仗仂仗舒 于 亠亞仂
wait-set. 亳 仂仄 于仗仂仍仆亠仆亳亠 舒从仂亞仂 仗仂仂从舒
仗亳仂舒仆舒于仍亳于舒亠 亟仂 亠 仗仂, 仗仂从舒 亟亞仂亶 仗仂仂从 仆亠
于亰仂于亠  仂亞仂 亢亠 仂弍亠从舒 仄亠仂亟 notifyAll(), 从仂仂亶
仗仂弍亢亟舒亠 于亠 仗仂仂从亳 亳亰 wait-set. 亠仂亟 notify() 仗仂弍亢亟舒亠
仂亟亳仆 仍舒亶仆仂 于弍舒仆仆亶 仗仂仂从 亳亰 亟舒仆仆仂亞仂 仆舒弍仂舒.
19
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
Wait-set 仄亠仂亟
亟仆舒从仂 仗亳仄亠仆亠仆亳亠 亳 仄亠仂亟仂于 于磶舒仆仂  仂亟仆亳仄 于舒亢仆仄
仂亞舒仆亳亠仆亳亠仄. ミ頴笑 亳亰 仆亳 仄仂亢亠 弍 于亰于舒仆 仗仂仂从仂仄 
仂弍亠从舒 仂仍从仂 仗仂仍亠 舒仆仂于仍亠仆亳 弍仍仂从亳仂于从亳 仆舒 仂
仂弍亠从. 丐仂 亠 仍亳弍仂 于仆亳 synchronized-弍仍仂从舒  仍从仂亶
仆舒 仂 仂弍亠从 于 从舒亠于亠 舒亞仄亠仆舒, 仍亳弍仂 仂弍舒亠仆亳 从
仄亠仂亟舒仄 亟仂仍亢仆 弍 于 亳仆仂仆亳亰亳仂于舒仆仆 仄亠仂亟舒
从仍舒舒 舒仄仂亞仂 仂弍亠从舒.

20
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
Wait-set 仄亠仂亟
public class WaitThread implements Runnable
{ private Object shared;
public WaitThread(Object o)
{ shared=o;}
public void run()
{ synchronized (shared) {
try { shared.wait(); } catch (InterruptedException e) {}
System.out.println("after wait");
}
}
21
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
Wait-set 仄亠仂亟
public static void main(String s[])
{Object o = new Object();
WaitThread w = new WaitThread(o);
new Thread(w).start();
try { Thread.sleep(100); } catch (InterruptedException e) {}
System.out.println("before notify");
synchronized (o) { o.notifyAll(); }
}
}
22
仆仂亞仂仗仂仂仆仂亠
仗仂亞舒仄仄亳仂于舒仆亳亠 仆舒 Java
Wait-set 仄亠仂亟
于仂亟 仗仂亞舒仄仄:
before notify
after wait

23

More Related Content

8. java lecture threads