Examples of static internal classes and synchronization classes in Java programming _java

Source: Internet
Author: User
Tags static class

The

Java static internal class
defines an inner class as a static class, which is essentially the same as the method of defining other classes as static classes. However, the details are still very different. Specifically, there are several places to draw attention to the program developers.
    (a) in general, if an inner class is not defined as a static inner class, it cannot be defined as a static member variable or a static member method when defining a member variable or a member method. In other words, static members cannot be declared in non-static inner classes.
    (ii) Generally non-static external classes can access the member variables of their external classes as well as methods (including methods declared private), but if an inner class is declared static, there are many restrictions on accessing the external class that includes itself. Static inner classes cannot access non-static member variables and methods of their external classes.
    (c) When creating a Non-static member inner class in a class, there is a compelling rule that an instance of an inner class must be bound to an instance of an external class. Then you define a static inner class in an external class, and you do not need to use the keyword new to create an instance of the inner class. That is, when you create an object inside a static class, you do not need an object of its outer class.
    Java uses the following internal classes when implementing 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; 
  } 
  ........ 
} 

Here is the typical usage of the static inner class


Java Synchronization Tools Class

/** * Need to start multiple threads to import the interface data into the target in batches, requirements * each time the implementation must ensure that the previous task has been completed, there are many ways to handle this requirement, the essence of the thread between the synchronization problem, just these two days I also focus on thread synchronization related Dongdong, the JDK provides a lot of threads * 
 Synchronization Tool class, Countdownlatch: A synchronization helper class that allows one or more threads to wait until a set of * operations is performed in another thread. * Initializes the Countdownlatch with the given count. Because the countdown () method is invoked, the await method remains blocked until the current count reaches 0. 
 After that, all waiting threads are freed and all subsequent calls to the await are returned immediately. * This phenomenon occurs only once-the count cannot be reset (this is important). 
 If you need to reset the count, consider using Cyclicbarrier. * Below is a simple example to simulate the demand, of course, because in order to simulate the scene, there will be some unreasonable place, here mainly elaborated * Countdownlatch synchronization, about the Countdownlatch source code will be analyzed in the back, 
It mainly involves Abstractqueuedsynchronizer * This class, his class capacity is relatively complex * **/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 {//Analog data Strlist = new arraylist<integer> (); 
 for (int i = 0; i < i++) {strlist.add (i); 
 } public static void Main (String args[]) {Boolean isend = true; 
 In order to verify correctness, only 20 times int count=0 is performed; 
 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 {//main thread block knows that all child threads will donesignal 0 donesignal.await (); 
   catch (Interruptedexception e) {e.printstacktrace (); 
    while (Strlist.size () <=0) {int pos = r.nextint (1000); 
    Strlist.clear (); 
    for (int i = pos; I < pos + 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; 
  The public void run () {try {//) waits 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) + "---" + "deleted"); 
 } 
  } 
 } 
 } 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.