As per Java specification, only public, abstract, default, static and strictfp are permitted for interface methods.
You may
like
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
class ClassLockEx implements Runnable{ static void printMethod(){ for(int i=0; i < 5; i++){ try{ Thread.currentThread().sleep(1000); } catch(Exception e){ System.out.println(e); } System.out.println(Thread.currentThread().getName() + " " + i); } } public void run(){ printMethod(); } public static void main(String args[])throws Exception{ ClassLockEx obj1 = new ClassLockEx(); ClassLockEx obj2 = new ClassLockEx(); Thread t1 = new Thread(obj1); Thread t2 = new Thread(obj2); t1.start(); t2.start(); } }
Thread-0 0 Thread-1 0 Thread-0 1 Thread-1 1 Thread-0 2 Thread-1 2 Thread-0 3 Thread-1 3 Thread-0 4 Thread-1 4
class ClassLockEx implements Runnable{ static synchronized void printMethod(){ for(int i=0; i < 5; i++){ try{ Thread.currentThread().sleep(1000); } catch(Exception e){ System.out.println(e); } System.out.println(Thread.currentThread().getName() + " " + i); } } public void run(){ printMethod(); } public static void main(String args[])throws Exception{ ClassLockEx obj1 = new ClassLockEx(); ClassLockEx obj2 = new ClassLockEx(); Thread t1 = new Thread(obj1); Thread t2 = new Thread(obj2); t1.start(); t2.start(); } }
Thread-0 0 Thread-0 1 Thread-0 2 Thread-0 3 Thread-0 4 Thread-1 0 Thread-1 1 Thread-1 2 Thread-1 3 Thread-1 4
class WithOutSynchronization implements Runnable{ public void run(){ sum(); } public void sum(){ for(int i=0; i < 1000000000; i++){ } } public static void main(String args[]) throws Exception{ WithOutSynchronization task1 = new WithOutSynchronization(); Thread t1 = new Thread(task1); Thread t2 = new Thread(task1); long time1 = System.currentTimeMillis(); t1.start(); t2.start(); t1.join(); t2.join(); long time2 = System.currentTimeMillis(); System.out.println("Time taken is " + (time2-time1)); } }
Time taken is 5
class WithSynchronization implements Runnable{ public void run(){ sum(); } public void sum(){ for(int i=0; i < 1000000000; i++){ synchronized(this){ } } } public static void main(String args[]) throws Exception{ WithSynchronization task1 = new WithSynchronization(); Thread t1 = new Thread(task1); Thread t2 = new Thread(task1); long time1 = System.currentTimeMillis(); t1.start(); t2.start(); t1.join(); t2.join(); long time2 = System.currentTimeMillis(); System.out.println("Time taken is " + (time2-time1)); } }
Time taken is 18014
class BucketDemo{ int stack[]; int top = -1, size=0; BucketDemo(int size){ stack = new int[size]; this.size = size; } synchronized boolean isSpaceAvailable(){ return (top < (size-1) ); } synchronized void push(int ele){ if(isSpaceAvailable()){ top = top + 1; stack[top] = ele; } } void display(){ for(int i=0; i < size; i++){ System.out.println(stack[i]); } } }
class BucketDemoThread1 implements Runnable{ BucketDemo obj; BucketDemoThread1(BucketDemo obj){ this.obj = obj; } public void run(){ for(int i=0; i < 10; i++){ obj.push(i); try { Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } } }
class BucketDemoThread2 implements Runnable{ BucketDemo obj; BucketDemoThread2(BucketDemo obj){ this.obj = obj; } public void run(){ for(int i=10; i < 20; i++){ obj.push(i); try{ Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } } }
class SynchronizeBlock { public static void main(String args[]) throws Exception{ BucketDemo obj = new BucketDemo(10); BucketDemoThread1 b1 = new BucketDemoThread1(obj); BucketDemoThread2 b2 = new BucketDemoThread2(obj); Thread task1 = new Thread(b1); Thread task2 = new Thread(b2); task1.start(); task2.start(); task1.join(); task2.join(); obj.display(); } }
0 10 1 11 12 2 13 3 4 14
class BucketDemoThread1 implements Runnable{ BucketDemo obj; BucketDemoThread1(BucketDemo obj){ this.obj = obj; } public void run(){ synchronized(obj){ for(int i=0; i < 10; i++){ obj.push(i); try { Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } } } }
class BucketDemoThread2 implements Runnable{ BucketDemo obj; BucketDemoThread2(BucketDemo obj){ this.obj = obj; } public void run(){ synchronized(obj){ for(int i=10; i < 20; i++){ obj.push(i); try{ Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } } } }
0 1 2 3 4 5 6 7 8 9
class SynchMethEx implements Runnable{ public void run(){ printA(); } synchronized void printA(){ for(int i=0; i < 5; i++){ System.out.println(Thread.currentThread().getName() + " A"); try{ Thread.currentThread().sleep(10); } catch(InterruptedException e){ } } } synchronized void printB(){ for(int i=0; i < 5; i++){ System.out.println(Thread.currentThread().getName() + " B"); try{ Thread.currentThread().sleep(10); } catch(InterruptedException e){ } } } public static void main(String args[]){ SynchMethEx task1 = new SynchMethEx(); Thread t1 = new Thread(task1); t1.setName("thread1"); t1.start(); task1.printB(); } }
main B main B main B main B main B thread1 A thread1 A thread1 A thread1 A thread1 A
class SynchMethEx implements Runnable{ public void run(){ printA(); } synchronized void printA(){ for(int i=0; i < 5; i++){ System.out.println(Thread.currentThread().getName() + " A"); try{ Thread.currentThread().sleep(10); } catch(InterruptedException e){ } } } void printB(){ for(int i=0; i < 5; i++){ System.out.println(Thread.currentThread().getName() + " B"); try{ Thread.currentThread().sleep(10); } catch(InterruptedException e){ } } } public static void main(String args[]){ SynchMethEx task1 = new SynchMethEx(); Thread t1 = new Thread(task1); t1.setName("thread1"); t1.start(); task1.printB(); } }
main B thread1 A main B thread1 A main B thread1 A main B thread1 A main B thread1 A
class PrintWithOutSynch implements Runnable{ public void run(){ print1to10(); } void print1to10(){ for(int i=0; i < 10; i++){ System.out.println(i); try{ Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } } public static void main(String args[]){ PrintWithOutSynch task1 = new PrintWithOutSynch(); Thread t1 = new Thread(task1); Thread t2 = new Thread(task1); t1.start(); t2.start(); } }
0 0 1 1 2 2 3 3 4 4
class PrintWithOutSynch implements Runnable{ public void run(){ print1to10(); } synchronized void print1to10(){ for(int i=0; i < 5; i++){ System.out.print(i +" "); try{ Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } } public static void main(String args[]){ PrintWithOutSynch task1 = new PrintWithOutSynch(); Thread t1 = new Thread(task1); Thread t2 = new Thread(task1); t1.start(); t2.start(); } }
0 1 2 3 4 0 1 2 3 4