9 public add(long value){
10 synchronized (Counter.class) {
15 public synchronized long getCounter() {
20 public class CounterThread extends Thread{
23 protected Counter counter;
25 public CounterThread(String name, Counter counter){
27 this.counter = counter;
31 for(int i=0; i<10; i++){
32 System.printString(this.name);
34 System.printString(" " + counter.getCounter() + "\n");
39 public class CounterS{
41 static long count = 1;
46 public long add(long value){
47 synchronized (CounterS.class) {
48 CounterS.count += value;
49 return CounterS.count;
53 public synchronized static long getCounter() {
54 return CounterS.count;
58 public class CounterSThread extends Thread{
61 protected CounterS counter;
63 public CounterSThread(String name, CounterS counter){
65 this.counter = counter;
69 for(int i=0; i<10; i++){
70 System.printString(this.name);
72 System.printString(" " + counter.getCounter() + "\n");
77 public class SynchonizedTest {
78 public SynchonizedTest() {
81 public static void main(String[] args){
82 Counter counter = new Counter();
83 Thread threadA = new CounterThread("A\n",counter);
84 Thread threadB = new CounterThread("B\n",counter);
89 CounterS countersA = new CounterS();
90 CounterS countersB = new CounterS();
91 Thread threadsA = new CounterSThread("C\n",countersA);
92 Thread threadsB = new CounterSThread("D\n",countersB);