Java Program to show Multi Threading
Java Threading program to show use of stop() method
Java Program to demonstrate Multi threading.
Java Program to demonstrate Thread Priority (MIN and MAX Priority)
Java Program to demonstrate Synchronized thread.
Java File handling example to show total no. of lines / strings present in the file.
Java Program to show Random Numbers
class Ta extends Thread { public void run() { for(int i=1; i<=5; i++) { System.out.println("A thread=" + i); } System.out.println("Ta is over"); } } class Tb extends Thread { public void run() { for(int j=1; j<=5; j++) { System.out.println("B thread=" + j); } System.out.println("Tb is over"); } } class thread { public static void main(String args[]) { Ta ta=new Ta(); ta.start(); Tb tb=new Tb(); tb.start(); } }
Java Threading program to show use of stop() method
class Ta extends Thread { public void run() { for(int i=1; i<=5; i++) { try { sleep(1000); } catch(Exception e) {} System.out.println("Thread A= " + i); } System.out.println("Exit of Thread A"); } } class Tb extends Thread { public void run() { for(int j=1; j<=5; j++) { if (j==3) stop(); System.out.println("Thread B= " + j); } } } class thread2 { public static void main(String args[]) { Ta ta=new Ta(); ta.start(); Tb tb=new Tb(); tb.start(); } }
Java Program to demonstrate Multi threading.
class Ta extends Thread { public void run() { for (int i=1; i<=5; i++) { try { sleep(1000); } catch(Exception e) {} System.out.println("Thread A is running="+i); } System.out.println("Thread A is Stopped"); } } class Tb extends Thread { public void run() { for (int j=1; j<=5; j++) { try { sleep(500); } catch(Exception e) {} System.out.println("Thread B is running="+j); } System.out.println("Thread B is Stopped"); } } class thread3 { public static void main(String args[]) { Ta ta=new Ta(); ta.start(); Tb tb=new Tb(); tb.start(); } }
Java Program to demonstrate Thread Priority (MIN and MAX Priority)
class Ta extends Thread { public void run() { for(int p=1; p<=5; p++) { try { sleep(500); } catch(InterruptedException e) { } System.out.println("Thread is running=p: "+p); } } } class Tb extends Thread { public void run() { for(int q=1; q<=5; q++) { try { sleep(500); } catch(InterruptedException e) { } System.out.println("Thread is running=q: "+q); } } } class thread5 { public static void main(String args[]) { Ta ta=new Ta(); Tb tb=new Tb(); tb.setPriority(Thread.MAX_PRIORITY); ta.setPriority(Thread.MIN_PRIORITY); ta.start(); tb.start(); System.out.println("Main thread is stopped"); } }
Java Program to demonstrate Synchronized thread.
class Tb extends Thread { int total; public void run() { synchronized(this) { for(int i=0; i<10; i++) { try { sleep(1000); } catch(Exception e) {} total += i; } notify(); } } } public class thread6 { public static void main(String args[]) { Tb b = new Tb(); b.start(); synchronized(b) { try { System.out.println("Waiting for b to complete the calculation..."); b.wait(); } catch(InterruptedException e){} System.out.println("Total is: " + b.total); } } }
Java File handling example to show total no. of lines / strings present in the file.
import java.io.*; public class NOL { public static void main(String[] args) { try { System.out.println("Getting line number of a paritcular file example!"); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please enter file name with extension:"); String str = bf.readLine(); File file = new File(str); if (file.exists()) { FileReader fr = new FileReader(file); LineNumberReader ln = new LineNumberReader(fr); int count = 0; while (ln.readLine() != null){ count++; } System.out.println("Total line no: " + count); ln.close(); } else{ System.out.println("File does not exists!"); } } catch(IOException e){ e.printStackTrace(); } } }
Java Program to show Random Numbers
import java.util.*; public class rannum { public static void main(String[] args) { Random rand = new Random(); int num = rand.nextInt(20); System.out.println("Generated Random Number between 0 to 20 is : " + num); int numNoRange = rand.nextInt(); System.out.println("Generated Random Number without specifying any range is : " + numNoRange); } }
No comments:
Post a Comment