The document contains Java programs demonstrating the use of ListIterator and Iterator for manipulating ArrayLists, including adding, removing, and checking elements. It also illustrates multithreading concepts such as inter-thread communication and thread priority, showcasing how threads can synchronize and notify each other. Additionally, the document includes examples of synchronized methods to manage access to shared resources between threads.
The document contains Java programs demonstrating the use of ListIterator and Iterator for manipulating ArrayLists, including adding, removing, and checking elements. It also illustrates multithreading concepts such as inter-thread communication and thread priority, showcasing how threads can synchronize and notify each other. Additionally, the document includes examples of synchronized methods to manage access to shared resources between threads.
import java.util.ArrayList; import java.util.ListIterator; public class Main { public static void main(String[] args) { // Create ArrayList object with capacity of 2 elements ArrayList al = new ArrayList(2); System.out.println(al+", size = "+al.size()); // Add items to the ArrayList al.add("R"); al.add("U"); al.add("O"); al.add(new String("x")); al.add(2, new Integer(10)); System.out.println(al+", size = " + al.size()); // Remove item al.remove("U"); System.out.println(al+", size = " + al.size()); // Check if the list contains the specified element Boolean b = al.contains("x"); System.out.println("The list contains x = " + b); b = al.contains("p"); System.out.println("The list contains p = " + b); b = al.contains(new Integer(10)); System.out.println("The list contains Integer of 10 = " + b); // Create ListIterator and iterate entries in it ListIterator li = al.listIterator(); while (li.hasNext()) System.out.println("From ListIterator = " + li.next()); // Create Object array from ArrayList Object a[] = al.toArray(); for (int i=0; i<a.length; i++) System.out.println("From an Array = " + a[i]); } }
Q2) Program to demonstrate Iterator
import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String[] args) { ArrayList al = new ArrayList(); for (int i = 0; i< 10; i++) al.add(i); System.out.println(al); // at beginning itr(cursor) will point to // index just before the first element in al Iterator itr = al.iterator(); // checking the next element availabilty while (itr.hasNext()) { // moving cursor to next element int i = (Integer)itr.next(); // getting even elements one by one System.out.print(i + " ");
// Removing odd elements
if (i % 2 != 0) itr.remove(); } System.out.println(); System.out.println(al); } } 2: Multithreading Q1) Demonstrating Inter-thread communication of a Thread class SampleThread extends Thread { int tBal = 0; public void run() { synchronized (this) { System.out.println("Thread calculation for total balance"); for (int i = 0; i <= 30; i++) { tBal = tBal + i; } System.out.println("Thread gives notification call"); this.notify(); } } } public class DemoThread { public static void main(String[] args) throws InterruptedException { SampleThread st = new SampleThread (); st.start(); synchronized (st) { System.out.println("Thread calling wait() Method"); st.wait(); System.out.println("Thread got notification"); System.out.println("Totol Balance " + st.tBal); } } } Q2) Demonstrating Priority of a Thread class PriorityDemo extends Thread { public void run() { System.out.println("running thread name is:"+Thread.currentThread().getName()); System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); } public static void main(String args[]) { PriorityDemo m1=new PriorityDemo(); PriorityDemo m2=new PriorityDemo(); m1.setPriority(Thread.MIN_PRIORITY); m2.setPriority(Thread.MAX_PRIORITY); m1.start(); m2.start(); } } class First { synchronized public void display(String msg) { System.out.print ("["+msg); try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println ("]"); } } class Second extends Thread { String msg; First fobj; Second (First fp,String str) { fobj = fp; msg = str; start(); } public void run() { fobj.display(msg); } } public class MyThread { public static void main (String[] args) { First fnew = new First(); Second ss = new Second(fnew, "welcome"); Second ss1= new Second(fnew,"new"); Second ss2 = new Second(fnew, "programmer"); } }