Java Threads Programs

ThreadDemo1.java

class A extends Thread{ public void run(){ for(int i=0;i<10;i++){ try{Thread.sleep(500);}catch(Exception e){} System.out.println(i+" A"); } } } class B extends Thread{ public void run(){ for(int i=0;i<10;i++){ try{Thread.sleep(500);}catch(Exception e){} System.out.println(i+" B"); } } } class ThreadDemo1 { public static void main(String ar[]){ //java.io.Console c=System.console(); //String s=c.readLine("Hello Enter something "); A a1=new A(); B b1=new B(); a1.start(); b1.start(); try{Thread.sleep(5500);}catch(Exception e){} a1.start(); System.out.println("Finished"); } }

ThreadDemo2.java

class A implements Runnable{ public void run(){ for(int i=0;i<10;i++){ try{Thread.sleep(500);}catch(Exception e){} System.out.println(i+" A"); } } } class B implements Runnable{ public void run(){ for(int i=0;i<10;i++){ try{Thread.sleep(500);}catch(Exception e){} System.out.println(i+" B"); } } } class ThreadDemo1 { public static void main(String ar[]){ //java.io.Console c=System.console(); //String s=c.readLine("Hello Enter something "); A a1=new A(); B b1=new B(); Thread t1=new Thread(a1); Thread t2=new Thread(b1); t1.start(); t2.start(); System.out.println("Finished"); } }

ThreadDemo3.java

class ThreadDemo3 { public static void main(String ar[]){ Thread t=Thread.currentThread(); System.out.println(t); t.setName("MyThread"); System.out.println(t.getName()); t.setPriority(7); System.out.println(t.getPriority()); } }

ThreadDemo4.java

class A implements Runnable{ public void run(){ for(int i=0;i<100000;i++){ //try{Thread.sleep(500);}catch(Exception e){} System.out.println(i+" A "+Thread.currentThread().getName()); } } } class B implements Runnable{ public void run(){ for(int i=0;i<100000;i++){ Thread t=Thread.currentThread(); //try{Thread.sleep(500);}catch(Exception e){} System.out.println(" "+i+"B "+t.getName()); } } } class ThreadDemo4 { public static void main(String ar[]){ //java.io.Console c=System.console(); //String s=c.readLine("Hello Enter something "); A a1=new A(); B b1=new B(); Thread t1=new Thread(a1); Thread t2=new Thread(b1); t1.setPriority(4); t2.setPriority(7); t1.setDaemon(true); t2.setDaemon(true); t1.start(); t2.start(); try{Thread.sleep(3000);}catch(Exception e){} System.out.println("Finished"); } }

ThreadDemo5.java

class A implements Runnable{ public void run(){ for(int i=0;i<100000;i++){ //try{Thread.sleep(500);}catch(Exception e){} if(i==100){ throw new ArithmeticException("Exception thrown"); } System.out.println(i+" A "+Thread.currentThread().getName()); } } } class B implements Runnable{ public void run(){ for(int i=0;i<100000;i++){ Thread t=Thread.currentThread(); try{ if(i==100){ throw new ArithmeticException("Exception thrown"); } }catch(ArithmeticException e){System.out.println("hi");} //try{Thread.sleep(500);}catch(Exception e){} System.out.println(" "+i+"B "+t.getName()); } } } class ThreadDemo4 { public static void main(String ar[]){ //java.io.Console c=System.console(); //String s=c.readLine("Hello Enter something "); A a1=new A(); B b1=new B(); Thread t1=new Thread(a1); Thread t2=new Thread(b1); t1.setPriority(4); t2.setPriority(7); t1.setDaemon(true); t2.setDaemon(true); t1.start(); t2.start(); try{Thread.sleep(3000);}catch(Exception e){} System.out.println("Finished"); } }

ThreadDemo6.java

class Common{ synchronized public void m1(){ System.out.println("Hi"); try{Thread.sleep(500);}catch(Exception e){} System.out.println("Hello"); } } class A implements Runnable{ Common c1; public void run(){ c1.m1(); } } class B implements Runnable{ Common c2; public void run(){ c2.m1(); } } class ThreadDemo4 { public static void main(String ar[]){ A a1=new A(); B b1=new B(); Common c=new Common(); a1.c1=c; b1.c2=c; Thread t1=new Thread(a1); Thread t2=new Thread(b1); t1.start(); t2.start(); } }

ThreadDemo7.java

class Common{ public void m1(){ System.out.println("Hi"); try{Thread.sleep(500);}catch(Exception e){} System.out.println("Hello"); } } class A implements Runnable{ Common c1; public void run(){ synchronized(c1){ c1.m1(); } } } class B implements Runnable{ Common c2; public void run(){ synchronized(c2){ c2.m1(); } } } class ThreadDemo7 { public static void main(String ar[]){ A a1=new A(); B b1=new B(); Common c=new Common(); a1.c1=c; b1.c2=c; Thread t1=new Thread(a1); Thread t2=new Thread(b1); t1.start(); t2.start(); } }

No comments:

Popular Posts