Skip to main content

Posts

Showing posts from May, 2020

Java program to print Thread in a sequential manner

Print Thread in a sequential manner package com.rohan.test; /**  *   * @author ROHAN There are 5 threads t1, t2, t3, t4, t5 we want to print 1 to 100 in ascending order but all these integers should be printed by above 5 threads in sequential order. Ex: t1 will print : 1 t2 will print : 2 t3 will print : 3 t4 will print : 4 t5 will print : 5 t1 will print : 6 t2 will print : 7  */ class PrintSequentialThread implements Runnable { static Object lock = new Object(); int totalnum = 100; static int num = 1; int sequence; PrintSequentialThread(int sequence) { this.sequence = sequence; } @Override public void run() { while(num <= totalnum-4) { synchronized(lock) { while(num % 5 != sequence) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+ " will print :...

Reverse array for the any generic type

Java program to reverse array for the any generic type: package com.rohan.ArrayUsingReflection; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ReverseArray { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("sbg"); list.add("Rohan"); list.add("Pankhuri"); for(String str : list) { System.out.println("list element: "+str); } System.out.println("=========================="); Iterator<String> itr = list.iterator(); while(itr.hasNext()) { System.out.println("list element: "+itr.next()); } System.out.println("=========================="); try { double [] vals = {1.1, 1.2, 1.3, 1.4, 1.5}; reverseArray(vals); for(double elem: vals) System.out.format(" %3.1f ", elem); Sy...