.


:




:

































 

 

 

 


, wait notify

. 2

. 3

synchronized. 4

wait notify. 6

, wait notify. 7

. 8

. 11

. 11

. 12

Runnable. 14

volatile. 15

: 16

 


, Thread:

Thread worker = new Thread();

- , . , . , start. start Thread, . start run , .

run . , stop; suspend; , .

Thread.run . Thread, run, Runnable .

, ping PONG :

class PingPong extends Thread {

String word; //

int delay; //

PingPong(String whatToSay, int delayTime) {

word = whatToSay;

delay = delayTime;

}

 

public void run() {

try {

for (;;) {

System.out.print(word + " ");

sleep(delay); //

}

} catch (InterruptedException e) {

return;

}

}

public static void main(String[] args) {

new PingPong("ping", 33).start(); // 1/30

new PingPong("PONG", 100).start(); // 1/10

}

}

 

PingPong. run , word delay . PingPong.run , Thread.run. , InterruptedException, sleep.

PingPong. PingPong, , start -. . :

ping PONG ping ping PONG ping ping ping PONG ping

ping PONG ping ping ping PONG ping ping PONG ping

ping ping PONG ping ping ping PONG ping ping PONG

ping ping ping PONG ping ping ping PONG ping ping

PONG ping ping ping PONG ping ping ping PONG ping

ping ping PONG ping ping PONG ping ping ping PONG...

 

, String , setName. , getName. runtime Java .

Thread.currentThread Thread, .

, synchronized. synchronized, . synchronized .

, . : , , , .

, : , , , .

, Account, :

class Account {

private double balance;

public Account(double initialDeposit) {

balance = initialDeposit;

}

public synchronized double getBalance() {

return balance;

}

public synchronized void deposit(double amount) {

balance += amount;

}

}

 

, .

synchronized, , . balance , synchronized. , public protected : .

, , . . , , . synchronized , ( ) . , ; , , , . , , , .

. . , . .

, . , . super.method() , .

synchronized

synchronized , , . synchronized : , . synchronized :

synchronized ()

. , . , , , . , synchronized:

/** */

public static void abs(int[] values) {

synchronized (values) {

for (int i = 0; i << values.length; i++) {

if (values[i] << 0)

values[i] = - values[i];

}

}

}

 

values . , synchronized. , - , values.

, , synchronized, . , , . - , synchronized, .

- , . , synchronized, . , , synchronized.

. , :

, , synchronized super.

synchronized , .

, synchronized. , - , synchronized .

wait notify

, , . : wait notify. wait , notify .

wait notify Object . , , . wait , (notify) , .

, wait notify. , , - :

synchronized void doWhenCondition() {

while (!)

wait();

... , ...

}

 

:

. . , , while , .

wait , (atomic) . , , . : , . . , .

. , . , while if.

, notify , , .

synchronized void changeCondition() {

... , ...

notify();

}

 

. notify , . , notifyAll.

. , :

class Queue {

//

Element head, tail;

 

public synchronized void append(Element p) {

if (tail == null)

head = p;

else

tail.next = p;

p.next = null;

tail = p;

notify(); //

}

 

public synchronized Element get() {

try {

while(head == null)

wait(); //

} catch (InterruptedException e) {

return;

}

 

Element p = head; //

head = head.next; //

if (head == null) // ,

tail = null;

return p;

}

}

 

. : ; ; null , get , - . , ( ).

, wait notify

wait notify. Object :

public final void wait(long timeout)

timeout. timeout . , -, .

public final void wait(long timeout, int nanos)

; - : timeout ( ) nanos ( , 0999999).

public final void wait()

wait(0).

public final void notify()

, . , , wait. , notify , , , . - , , notifyAll.

public final void notifyAll()

, . , - . , . , , wait.

Object. , , . , , . , , IllegalMonitorState Exception.

Java , , , . , (runnable) . . / (lock), , (block). , , . - . ./ , , .

, ( ). Java ( , ) .

Runtime- Java , , , , , . , . , . , .

, . setPriority , MIN_PRIORITY MAX_PRIORITY Thread. NORM_PRIORITY. . , , . getPriority .

, , , . , STOP, , . , , , . , , , . . , , MIN_PRIORITY, .

Thread :

public static void sleep(long millis)

. , . , , .

public static void sleep(long millis, int nanos)

. 0999999.

public static void yield()

, . . , , .

yield. , . , println; . , :

class Babble extends Thread {

static boolean doYield; // ?

Static int howOften; //

String word; //

Babble(String whatToSay) {

word = whatToSay;

}

 

public void run() {

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

System.out.println(word);

if (doYield)

yield(); //

}

}

 

public static void main(String[] args) {

howOften = Integer.parseInt(args[1]);

doYield = new Boolean(args[0]).booleanValue();

 

//

//

Thread cur = currentThread();

cur.setPriority(Thread.MAX_PRIORITY);

for (int i = 2; i << args.length; i++)

new babble(args[i]).start();

}

}

, , , . , doYield false:

Babble false 2 Did DidNot

:

Did

Did

DidNot

DidNot

 

println, . doYield true:

Babble true 2 Did DidNot

, , , :

Did

DidNot

Did

DidNot

. , . yield .

(suspended), , . , CANCEL . , ( ) . :

Thread spinner; //,

 

public void userHitCancel() {

spinner.suspend(); //

if (askYesNo("Really Cancel?"))

spinner.stop(); //

else

spinner.resume(); // !

}

 

userHitCancel suspend , , . , . , stop ; resume .

, , , .

Thread (interrupting) . , Java. ; NoSuchMethodError . , , , . .

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

, (, sleep wait) InterruptedException. , Interrupted Exception.

. interrupt ; isInterrupted ; interrupted , .

, run. , -.

, , , : , . ,

stop, ThreadDeath, . ThreadDeath Error, Exception ( , , ). ThreadDeath, - , finally. ThreadDeath, - , . ThreadDeath , , .

ThreadDeath , . , run run , .

stop ThreadDeath - . , , - . , , . , . , , Restart Calculation stop, . , , .

. join. :

class CalcThread extends Thread {

private double Result;

 

public void run() {

Result = calculate();

}

 

public double result() {

return Result;

}

 

public double calculate() {

//...

}

}

 

class join {

public static void main(String[] args) {

CalcThread calc = new CalcThread();

calc.start();

doSomethingElse();

try {

calc.join();

System.out.println("result is "

+ calc.result());

} catch (InterruptedException e) {

System.out.println("No answer: interrupted");

}

}

 

}

, CalcThread, . , , (join) . join , CalcThread.run , Result . , CalcThread doSomethingElse . , , .

join -, , sleep. join:

public final void join()

, .

public final synchronized void join(long millis)

( , ). , , -.

public final synchronized void join(long millis, int nanos)

- . -, 0 , -. 0999999.

destroy . , , destroy . destroy.

Runnable

Runnable , . Runnable :

public void run();

Thread Runnable, . , - Thread, . , , , , Thread . , , , Thread.

Runnable. Runnable Thread. Thread Runnable, Thread.run run .

PingPong, Runnable. , . (Runnable Thread) main:

class RunPingPong inplements Runnable {

String word; //

int delay; //

PingPong(String whatToSay, int delayTime) {

word = whatToSay;

delay = delayTime;

}

 

public void run() {

try {

for (;;) {

System.out.print(word + " ");

Thread.sleep(delay); //

//

}

} catch (InterruptedException e) {

return; //

}

}

 

public static void main(String[] args) {

Runnable ping = new RunPingPong("ping", 33);

Runnable pong = new RunPingPong("PONG", 100);

}

}

, Runnable. run PingPong. main RunPingPong ; Thread.

Thread, Runnable:

public Thread(Runnable target)

Thread, run target.

public Thread(Runnable target, String name)

Thread name, run target.

public Thread(ThreadGroup group, Runnable target)

Thread, ThreadGroup run target.

public Thread(ThreadGroup group, Runnable target, String name)

Thread name, ThreadGroup run target.

volatile

, , , . ( , ), volatile. , , , :

currentValue = 5;

for (;;) {

display.showValue(currentValue);

Thread.sleep(1000); // 1

}

 

currentValue ShowValue, , currentValue , 5 showValue.

, currentValue , . currentValue volatile .


:

1. . ( ) . , - . + . .

2. . , -. . . . , , ( ).

3. / String. :

a. , ().

b. , .

c. , .

d. , - .

e.



<== | ==>
: . NOD | :
:


: 2016-09-03; !; : 561 |


:

:

,
==> ...

1539 - | 1414 -


© 2015-2024 lektsii.org - -

: 0.199 .