



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















!
!
class AtomicCounter {
private int count;AtomicCounter (int n) {
count = n;
} public void increment() {
synchronized
(this) {
count++;
}
} public int getCount() {
int c; synchronized
(this) {
c = count;
} return c;
}
}
class AtomicCounter {
private int count;AtomicCounter (int n) {
count = n;
} synchronized
public void increment() {
count++;
} synchronized
public int getCount() {
int c;
c = count;
return c;
}
}
extend Thread
run()
& )
" ' &
start()
class CountUp extends Thread {
public void run () {
counter.increment();System.out.println ("count = " +
counter.getCount());
} static AtomicCounter counter
= new AtomicCounter (0);
} public class testme {
public static void main (String args[]) {for (int i = 0; i < 100; i++) {
CountUp f = new CountUp();f.start();
}
}
class CountUp extends Thread {
public void run () {
synchronized
(counter) {
counter.increment();System.out.println ("count = " +
counter.getCount());
}
} static AtomicCounter counter
= new AtomicCounter (0);
} public class testme2 {
public static void main (String args[]) {for (int i = 0; i < 100; i++) {
CountUp f = new CountUp();f.start();
}
}
! "
"!
!
Thread
$ %
4
& !) 5 ) !!
! /
! !
)
class CountUp extends Thread {
public void run () {
synchronized (counter) {
counter.increment();System.out.println ("count = " +
counter.getCount());
}
} private
AtomicCounter counter
= new AtomicCounter (0);
}
& ,& !) & ) +
setPriority(int)
getPriority()
yield()
( & & !) 2
t.join()
!5 & !)
$
$
2 " 5
0 8
'
!
9,:
) ) & #!"!
;19;:
(!<
$ !
&
/ 0
"!
$
'
1 (
5
!"
& !& !
9
,& :
$. .'
$(
-) "
/
(
(
) +
(
'
( ) * +
2
0+ / 0
" 3
'
put()
55
/
poll()
5
! & !)
+ . (
!
2!
!
! +
'
( ) * -
$ ) (3
! &
put()
!5
take()
+ *& ( )
2!
!
! +
! $ 5& !) !!'
'
( ) * .
$/ 0
" 3
6 $ ) ) . /
$ !) & !
!) 4 &
9 :
2!
!
! +
1 4