Java puzzle Eight: A lot of other library mysteries

Source: Internet
Author: User

This article is based on the Java doubts this book, made notes.
ebook See:http://download.csdn.net/detail/u010378705/7527721 Puzzle
Start the thread starting method (), written as run ();
PS: Enhancement (monitor) lock needs further understanding.
Puzzle the issue of locking in Threads. Understanding is not profound.

Puzzle 78

reflection can cause access to members of non-public types in other packages. Causes the execution period to be abnormal.
Puzzle
Masking: The Thread.Sleep () method obscures the custom method.
Puzzle
Reflection: How to instantiate a non-static inner class and a static inner class.

The
Riddle of Bayi
System.out.write (): Does not voluntarily refresh itself. Need to use System.out.flush () to refresh.
Puzzle
the problem of process use is not understood.
The riddle of the
puzzle
The relationship between singleton patterns and serialization.
public class Dog extends Exception {private static final long Serialversionuid = 1l;publ IC static final Dog INSTANCE = new Dog ();p rivate Dog () {}public String toString () {return ' Woof ';}
 Private Object Readresolve () {return INSTANCE;}} 
Exception implements the serializable, there must be a Readresolve method to return its unique instance.
Otherwise it can be deserialized to get multiple objects.

Puzzle
thread.interrupted () method. Clears the interrupt state of the current thread.
The thread.isinterrupted () method is simply the query interrupt state.

Puzzle
Waiting for a background thread during the initialization of a class is very likely to cause a deadlock.

public class Lazy {private static Boolean initialized = false;static {thread T = new Thread (new Runnable () {public void Ru N () {initialized = true;}}); T.start (); try {t.join ();} catch (Interruptedexception e) {e.printstacktrace ()}} public static void Main (string[] args) {System.out.println (initialized);}}


1. When the main thread calls the Lazy.main method, it checks whether the lazy class has been initialized, not at this time, so the main thread will record that the initialization is currently in progress. and initialize the class.
2. The main thread sets the value of initialized to False to create a background thread. The change background thread sets initialized to true, at which point the main thread waits for the background threads to finish running.

3. However, when the background thread calls the Run method. Before lazy.initialized is set to true, the lazy class is also checked for initialization.
This class is being initialized by the main thread.

The background thread waits until the initialization is complete.

4. The main thread is also waiting for the background thread to end. Cause a deadlock.

Java puzzle Eight: A lot of other library mysteries

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.