Untitled

 avatar
unknown
java
3 years ago
718 B
2
Indexable
package examall;

class Counter{
	int count=0;
	synchronized void increment(){
		count++;
	}
}
public class b1 {//Synchronization
	public static void main(String[] args) throws InterruptedException {
		Counter obj= new Counter();
		Thread t1=new Thread(new Runnable(){//This method is called Annonymous inner class
			public void run(){
				for (int i=1; i<=1000 ; i++ ) {
					obj.increment();//This method is called Annonymous inner class	
				}
			}
		});
		Thread t2 = new Thread(new Runnable(){
			public void run(){
				for(int i=1; i<=1000;i++){
					obj.increment();
				}
			}
		});
		t1.start();
		t2.start();
		t1.join();
		t2.join();
		System.out.println(obj.count);
		
	}
}