Examples of static inner and synchronous classes in Java programming

  • 2020-04-01 04:10:55
  • OfStack

Java static inner class
Defining an inner class as a static class is basically the same as defining other classes as static classes, and the reference rules are basically the same. But the details are still quite different. Specifically, there are several places to attract the attention of developers.
      (a) in general, if an inner class is not defined as a static inner class, then when defining a member variable or a member method, it cannot be defined as a static member variable and a static member method. That is, you cannot declare static members in a non-static inner class.
      (2) generally, non-static external class can access the member variables and methods of its external class at will (including methods declared as private), but if an internal class is declared as static, it will have many restrictions on accessing the external class including itself. A static inner class cannot access non-static member variables and methods of its outer class.
      (three) in a class to create a non-static member of the inner class, there is a mandatory provision that the instance of the inner class must be bound to the instance of the outer class. You then define a static inner class in an outer class, without using the new keyword to create an instance of the inner class. That is, when creating a static class internal object, it does not need the object of its external class.
      Java USES the following inner class to implement LinkedList:


public class LinkedList<E> 
  extends AbstractSequentialList<E> 
  implements List<E>, Deque<E>, Cloneable, java.io.Serializable 
{ 
  ........ 
  private static class Entry<E> { 
E element; 
Entry<E> next; 
Entry<E> previous; 
Entry(E element, Entry<E> next, Entry<E> previous) { 
  this.element = element; 
  this.next = next; 
  this.previous = previous; 
} 
  } 
  private Entry<E> addBefore(E e, Entry<E> entry) { 
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); 
newEntry.previous.next = newEntry; 
newEntry.next.previous = newEntry; 
size++; 
modCount++; 
return newEntry; 
  } 
  ........ 
} 

This is the typical use of a static inner class


Java synchronization utility classes


 
 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import java.util.concurrent.CountDownLatch; 
 
public class Driver { 
 static List<Integer> strList = null; 
 int k = 0; 
 static { 
 //Simulated data
 strList = new ArrayList<Integer>(); 
 for (int i = 0; i < 50; i++) { 
  strList.add(i); 
 } 
 } 
 
 public static void main(String args[]) { 
 boolean isEnd = true; 
 //To verify correctness, execute only 20 times
 int count=0; 
 Driver d = new Driver(); 
 while (isEnd && strList.size() > 0&&count<20) { 
  CountDownLatch startSignal = new CountDownLatch(1); 
  final CountDownLatch doneSignal = new CountDownLatch(5); 
  for (int i = 0; i < 5; ++i) 
  { 
  new Thread(d.new Worker(startSignal, doneSignal,i)).start(); 
  } 
  //Count minus 1 child thread Worker can execute
  startSignal.countDown(); 
  try { 
  new Thread(new Runnable() { 
   Random r = new Random(); 
   @Override 
   public void run() { 
   try { 
    //The main thread block knows that all child threads have doneSignal cleared
    doneSignal.await(); 
   } catch (InterruptedException e) { 
    e.printStackTrace(); 
   } 
   while(strList.size()<=0){ 
    int pos = r.nextInt(1000); 
    strList.clear(); 
    for (int i = pos; i < pos + 50; i++) { 
    strList.add(i); 
    } 
   } 
   } 
  }).start(); 
  isEnd = true; 
  } catch (Exception e) { 
  e.printStackTrace(); 
  } 
  count++; 
 } 
 } 
 
 class Worker implements Runnable { 
 private final CountDownLatch startSignal; 
 private final CountDownLatch doneSignal; 
 private int i; 
 
 Worker(CountDownLatch startSignal, CountDownLatch doneSignal,int i) { 
  this.startSignal = startSignal; 
  this.doneSignal = doneSignal; 
  this.i=i; 
  
 } 
 
 public void run() { 
  try { 
  //Wait for the main thread to execute countDown
  startSignal.await(); 
  doWork(); 
  //Count minus 1
  doneSignal.countDown(); 
  } catch (InterruptedException ex) { 
  } // return; 
 } 
 
 void doWork() { 
  synchronized (strList) { 
  int start=(i)*(50/5); 
  int end=(i+1)*(50/5); 
  for (int i = start; i < end; i++) { 
   System.out.println(strList.get(i) + "---" + " Has been deleted "); 
  } 
  } 
 } 
 } 
} 

Related articles: