Вы находитесь на странице: 1из 26

Java

12. .

20 2003
:
( Sun ) <vyazovick@itc.mipt.ru>
( Sun ) < gene@itc.mipt.ru>
Copyright 2003 Sun , , .

Java.

.
- , ,
, .
, , Java.

, , , . ,
, .

Rendered by www.RenderX.com

ii

12. . ............................................................. 1
1. .......................................................................................................................... 1
2. ........................................................................................... 2
3. ....................................................................... 4
3.1. Thread........................................................................................................... 4
3.2. Runnable.............................................................................................. 5
3.3. ..........................................................................................5
3.4. -.......................................................................................................... 8
4. .............................................................................................................. 11
4.1. ......................................................................... 13
4.2. volatile............................................................................................ 14
4.3. ............................................................................................................ 15
5. wait(), notify(), notifyAll() Object............................................................19
6. ...................................................................................................21

Java

Rendered by www.RenderX.com

. 1 24

12. .

.
1. ......................................................................................................................... 1
2. .......................................................................................... 2
3. ...................................................................... 4
3.1. Thread................................................................................................................. 4
3.2. Runnable.................................................................................................... 5
3.3. ................................................................................................5
3.4. -................................................................................................................ 8

4. ..............................................................................................................11
4.1. ............................................................................... 13
4.2. volatile..................................................................................................14
4.3. ..................................................................................................................15

5. wait(), notify(), notifyAll() Object........................................................... 19


6. .................................................................................................. 21

1.
,
, .
, Java , ..
(threads) .
,
.
Java,
.

, ,
.
,
, , . Java

Java

Rendered by www.RenderX.com

. 2 24

synchronized.
.
wait(), notify(), notifyAll() Object.

2.
,
, .
,
.
, .
.
-
,
?
(time-slicing). .
,
.
.
,
. ,
( ),
. , Windows
, .
.
-
?
,
. , ,
. ,
,
() . , ,
, , .
, .
, ,
, .
, ,
.
, ,
.
- , .
,
.
.
Java

Rendered by www.RenderX.com

. 3 24

,
.
- , -
. time-slicing, -
, , , , , , ,
, , .
,
.
Java ,
.
,
. - ,
. ,
, , ( ..) . ,
.
, ,
, ,
- . , ,
,
. ,
, , 2 , ,
2 ,
.
(, ),
,
, .
-
. , , ,
, , ,
2 . , ,
, - ( ).
()
( ,
), . ,
,
.
(priority) . Java
. , .
, -
. -
,
.
,
. ,

Java

Rendered by www.RenderX.com

. 4 24

.
,
.
. ,
,
. ,
.
, ,
. , Java
() . - ,
, , .
, garbage collector' .
(daemon),
. ,
-.
, Java.

3.
3.1. Thread
Java Thread. ,

run(). ,
public class MyThread extends Thread {
public void run() {
// ,
long sum=0;
for (int i=0; i<1000; i++) {
sum+=i;
}
System.out.println(sum);
}
}

run() , .
, -,
start(), ,
run().
MyThread t = new MyThread();
t.start();

Java

Rendered by www.RenderX.com

Runnable

. 5 24

:
499500

run() ( , return)
. , .
,
.

3.2. Runnable
. Java
, Thread
. , ,
run().
.
Runnable, -
void run(). , , :
public class MyRunnable implements Runnable {
public void run() {
// ,
long sum=0;
for (int i=0; i<1000; i++) {
sum+=i;
}
System.out.println(sum);
}
}

:
Runnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();

, , run(),
,
MyThread, . ,
.
, Runnable Thread,
Thread.start().

3.3.
, Java . Thread
getPriority() setPriority(), :

Java

Rendered by www.RenderX.com

. 6 24

MIN_PRIORITY
MAX_PRIORITY
NORM_PRIORITY

, ,
( ) .
:
public class ThreadTest implements Runnable {
public void run() {
double calc;
for (int i=0; i<50000; i++) {
calc=Math.sin(i*i);
if (i%10000==0) {
System.out.println(getName()+" counts " + i/10000);
}
}
}
public String getName() {
return Thread.currentThread().getName();
}
public static void main(String s[]) {
//
Thread t[] = new Thread[3];
for (int i=0; i<t.length; i++) {
t[i]=new Thread(new ThreadTest(), "Thread "+i);
}
//
for (int i=0; i<t.length; i++) {
t[i].start();
System.out.println(t[i].getName()+" started");
}
}
}

Thread:

getName()
, Thread .
Runnable . ,
. .
, Java "Thread-"
( ) getName().
setName().

Java

Rendered by www.RenderX.com

. 7 24

currentThread()

Thread, .

:
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread

0
1
2
0
1
2
0
1
2
0
2
1
2
0
1
2
0
1

started
started
started
counts 0
counts 0
counts 0
counts 1
counts 1
counts 1
counts 2
counts 2
counts 2
counts 3
counts 3
counts 3
counts 4
counts 4
counts 4

, ,
. , ,
. , ,
.
,
, . main()
public static void main(String s[]) {
//
Thread t[] = new Thread[3];
for (int i=0; i<t.length; i++) {
t[i]=new Thread(new ThreadTest(), "Thread "+i);
t[i].setPriority(Thread.MIN_PRIORITY +
(Thread.MAX_PRIORITY-Thread.MIN_PRIORITY)/t.length*i);
}
//
for (int i=0; i<t.length; i++) {
t[i].start();
System.out.println(t[i].getName()+" started");
}
}

Java

Rendered by www.RenderX.com

. 8 24


. ,
1, - 10, - 5.
, ,
3.
:
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread

0
1
2
2
2
2
2
2
0
1
1
1
1
1
0
0
0
0

started
started
started
counts 0
counts 1
counts 2
counts 3
counts 4
counts 0
counts 0
counts 1
counts 2
counts 3
counts 4
counts 1
counts 2
counts 3
counts 4

, , . ,
, . ,
(Thread 0)
, (Thread 1).
, ,
. ,
.
( 500.000 , 50.000,
1000 , 10.000), ,
,
.

3.4. -
- ,
.
setDaemon() isDaemon().
:
public class ThreadTest implements Runnable {

Java

Rendered by www.RenderX.com

. 9 24

// ,
// ThreadTest
public final static ThreadGroup GROUP =
new ThreadGroup("Daemon demo");
// ,
//
private int start;
public ThreadTest(int s) {
start = (s%2==0)? s: s+1;
new Thread(GROUP, this, "Thread "+start).start();
}
public void run() {
//
for (int i=start; i>0; i--) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {}
//
//
if (start>2 && i==start/2) {
new ThreadTest(i);
}
}
}
public static void main(String s[]) {
new ThreadTest(16);
new DaemonDemo();
}
}
public class DaemonDemo extends Thread {
public DaemonDemo() {
super("Daemon demo thread");
setDaemon(true);
start();
}
public void run() {
Thread threads[]=new Thread[10];
while (true) {
//
//
int count=ThreadTest.GROUP.activeCount();
if (threads.length<count) threads =
Java

Rendered by www.RenderX.com

. 10 24

new Thread[count+10];
count=ThreadTest.GROUP.enumerate(threads);
//
for (int i=0; i<count; i++) {
System.out.print(threads[i].getName()+", ");
}
System.out.println();
try {
Thread.sleep(300);
} catch (InterruptedException e) {}
}
}
}

. ThreadTest
, . run()
.
.
. main()
16. , , 8,
4, 2.
- DaemonDemo.
ThreadTest
.
:
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread

16,
16,
16,
16,
16,
16,
16,
16,
16,
16, Thread 8,
16, Thread 8,
16, Thread 8,
16, Thread 8,
16, Thread 8,
16, Thread 8, Thread 4,
16, Thread 8, Thread 4,
8, Thread 4,
4, Thread 2,
2,

, - run(),
-- .
Java

Rendered by www.RenderX.com

. 11 24

,
:

ThreadGroup
, ThreadGroup.
. ,
, , .
activeCount() enumerate()
.

sleep()
Thread
. ,
InterruptedException. ,
. ,
sleep(), ,
, interrupt()
. , sleep() InterruptedException.

sleep() yield() .
,
.
, ,
.

4.
,
,
.
, .
, ,
.
:
public class ThreadTest

private int a=1, b=2;


public void one() {
a=b;
}
public void two() {
b=a;
}
public static void main(String s[]) {
int a11=0, a22=0, a12=0;

Java

Rendered by www.RenderX.com

. 12 24

for (int i=0; i<1000; i++) {


final ThreadTest o = new ThreadTest();
// ,
//
new Thread() {
public void run() {
o.one();
}
}.start();
// ,
//
new Thread() {
public void run() {
o.two();
}
}.start();
//
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
//
if (o.a==1 && o.b==1) a11++;
if (o.a==2 && o.b==2) a22++;
if (o.a!=o.b) a12++;
}
System.out.println(a11+" "+a22+" "+a12);
}
}


, one() two().
a b , . ,
1 2 , , ,
, . ,
, . :
135 864 1

, 1.
2.
. ,
1 2 . ,
, !
, 10.000, ,
:
Java

Rendered by www.RenderX.com

. 13 24

494 9498 8

,
:
0 3 997

, .
, ,
, .
,
Thread.sleep().
, ,
.
Java
.

4.1.
(main storage),
.
, .
,
, .
(working memory),
.
, :

use -

assign -

read -

load - , ,

store -

write - ,
store

, - ,
.
.
, , use assign
. ,
/ . .
Java

Rendered by www.RenderX.com

. 14 24

read,
load.
, .. ,
.
store, write.
, ..
, .
,
,
(, ,
..).
:

, , ..

, ,
, ..


. , , ,

.

.
,
.
, ,
.

4.2. volatile
volatile.
.
use volatile , ,
load,
- load use. ,
.
, store volatile ,
, assign,
- assign store.
,
.
, volatile ,

.

Java

Rendered by www.RenderX.com

. 15 24

.
,
, ,
.
64- double long.
32- ,
,
. , ,
. volatile
.

4.3.
(lock),
- (lock) (unlock).
. ,
unlock, ,
, .
lock unlock
. lock, ,
. ,
unlock .
, - .
, , ,
- . ,
- ,
unlock.
Java- , ,
synchronized. -
synchronized- .
.
Synchronized- :
synchronized (ref) {
...
}

, ,
, ref (
null). ,
, lock.
.
, unlock, .

Java

Rendered by www.RenderX.com

. 16 24

:
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();
}
}
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();
}
}
}

,
. :
Thread-0
Thread-1
Thread-2
Thread-0
Thread-2
Thread-0
Thread-1
Thread-2
Thread-1

0
0
0
1
1
2
1
2
2

, .
synchronized-:
public void run() {
synchronized (shared) {
shared.process();
}
}

Java

Rendered by www.RenderX.com

. 17 24

:
Thread-0
Thread-0
Thread-0
Thread-1
Thread-1
Thread-1
Thread-2
Thread-2
Thread-2

0
1
2
0
1
2
0
1
2

Synchronized- . ,
, .
.
, synchronized-, process()
.
static synchronized .
Class, , .
deadlock , .
, ,
,
.
:
public class DeadlockDemo {
// -
public final static Object one=new Object(), two=new Object();
public static void main(String s[]) {
// ,
// one two
Thread t1 = new Thread() {
public void run() {
//
synchronized(one) {
Thread.yield();
//
synchronized (two) {
System.out.println("Success!");
}
}
}
};
Thread t2 = new Thread() {
Java

Rendered by www.RenderX.com

. 18 24

public void run() {


//
synchronized(two) {
Thread.yield();
//
synchronized (one) {
System.out.println("Success!");
}
}
}
};
//
t1.start();
t2.start();
}
}

, .
, yield() . ,
,
. , "",
. ,
. " ", deadlock.
,
. ,
. (
yield()) (
), ( ""),
(-
), .
Java deadlock.
,
. ,
. ,
(
, ), .

. , ,
sleep(..), ,

, deadlock.
,
.

Java

Rendered by www.RenderX.com

. 19 24

5. wait(), notify(), notifyAll() Object


, Object,
Java.
Java synchronized ,
wait-set, .
wait() wait-set.
,
notifyAll(), wait-set. notify()
, .
.

. , synchronized-
,
. :
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");
}
}
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();
}
}
}

Java

Rendered by www.RenderX.com

. 20 24

wait(), notify(), notifyAll() Object

:
before notify
after wait

, wait(), sleep(), InterruptedException, interrupt().


:
public class ThreadTest implements Runnable {
final static private Object shared=new Object();
private int type;
public ThreadTest(int i) {
type=i;
}
public void run() {
if (type==1 || type==2) {
synchronized (shared) {
try {
shared.wait();
} catch (InterruptedException e) {}
System.out.println("Thread "+type+
" after wait()");
}
} else {
synchronized (shared) {
shared.notifyAll();
System.out.println("Thread "+type+
" after notifyAll()");
}
}
}
public static void main(String s[]) {
ThreadTest w1 = new ThreadTest(1);
new Thread(w1).start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
ThreadTest w2 = new ThreadTest(2);
new Thread(w2).start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {}

Java

Rendered by www.RenderX.com

. 21 24

ThreadTest w3 = new ThreadTest(3);


new Thread(w3).start();
}
}

:
Thread 3 after notifyAll()
Thread 1 after wait()
Thread 2 after wait()

, . -, 1,
wait() .
2. 3.
. 1 synchronized, shared. ,
2 synchronized-, 3.
, "" 1 2?
, ,
wait(), . , notifyAll().
, wait-set .
,
synchronized-!
, notifyAll() .
.
synchronized- ,
. - ,
wait(), notifyAll().
.
wait() , -,
.
.

6.
12-1.
?
a.)
. ,
.
, ,
. time-slicing.
12-2. ?

Java

Rendered by www.RenderX.com

. 22 24

a.) :


(, ,
), ,
, ,
.

,
(, , ,
, ..),
,
.


, .
,
.

12-3. ?
a.) , , ,
.

,
, .
12-4. -?
a.) - . -
, ,
-.
12-5. ,
?
a.) -,
.
12-6. Java Thread?
a.) Thread ,
(, ,
..).
12-7. Runnable
,
Thread?
a.) ,
Thread .
.
12-8. Java? ?
Java

Rendered by www.RenderX.com

. 23 24

a.) JVM ,
, .
. ,
, ,
.
. ,
, .
12-9. synchronized-,
, ? ?
a.) , synchronized . synchronized-
, .
12-10. synchronized, ?
a.) ,
.
12-11. static synchronized ?
a.) synchronized ,
, Class,
. , ,
static synchronized
.
12-12. wait InterruptedException, notify notifyAll
?
a.) wait ,
, interrupt(),
InterruptedException.
notify notifyAll .
12-13. wait,
notify? notifyAll?
a.) wait
,
,
. - ,
wait.
12-14. ?
public abstract class Test implements Runnable {
private Object lock = new Object();
public void lock() {
synchronized (lock) {
Java

Rendered by www.RenderX.com

. 24 24

try {
lock.wait();
System.out.println(1);
} catch (InterruptedException e) {
}
}
}
public void unlock() {
synchronized (lock) {
lock.notify();
System.out.println(2);
}
}
public static void main(String s[]) {
new Thread(new Test() {
public void run() {
lock();
}
}).start();
new Thread(new Test() {
public void run() {
unlock();
}
}).start();
}
}

a.) 2,
,
wait. notify,
( lock),
.

Java

Rendered by www.RenderX.com