Java from cainiao to proficient (10)

Source: Internet
Author: User

Singleton design mode.
1. Hungry Chinese

/*
Class Single {
// Write final more strictly, and s always points to single ()
Private Static [Final] single S = new

Single ();
Private single (){}
Public static single getinstance (){
Return S;
}
}

*/
***********************
************************
************************
2. Lazy *** interview ****
// Security risks may occur during multi-threaded access
1. What is the difference between lazy and hungry Chinese?
Lazy features: used for delayed loading of instances
2. Is there any problem with the delayed loading of the instance?
Yes. Security issues may occur during multi-threaded access.
3. How can this problem be solved?
You can use synchronization to solve the problem. In addition, the synchronous mode is used.

Or Synchronous Code block, but it is a little inefficient.
4. How to Solve inefficiency?
The dual judgment form is used to solve the inefficiency problem.
5. Which lock is used when synchronization is applied?
The bytecode object to which the class belongs.
6. Pen questions: Write a delayed loading Singleton design mode instance

. (As shown below)
Class Single {
Private Static Single S = NULL;
Private single (){}
Public static [synchronized] single

Getinstance (){
If (S = NULL ){
Synchronized (single. Class ){
If (S = NULL ){
// --> Thread
S = new single ();
}
}
}
Return S;
}
}

Deadlock.
That is to say, a holds a lock, B also holds a lock, and a wants to go to B.

Face to run, to get the lock of B, and B to run in a, must

Take the Lock of A. In this case, a will not put its own lock and go to B.

Running, B does not put its own lock, and no one is running in.

And cause a deadlock. In this case, the program will not

Run.
The most likely deadlock is: Nested synchronization in synchronization, but different locks

.
* ************ Interview ******
Please write a deadlock program?
Deadlock example:
Class test implements runnable
{
Private Boolean flag;
Test (Boolean flag ){
This. Flag = flag;
}
Public void run (){

If (FLAG ){
While (true ){
Synchronized

(Mylock. Locka ){
Syso ("If Testa ")
Synchronized

(Mylock. lockb ){
Syso ("else testb ");
}
}
}
} Else {
While (true ){
Synchronized

(Mylock. lockb ){
Syso ("If testb ");
Synchronized

(Mylock. Locka ){
Syso ("else Testa ");
}
}
}
}
}
}

Class mylock
{
Static object Locka = new object ();
Static object lockb = new object ();

}

Class deadlocktest {

Public static void main (string []

ARGs ){
Thread T1 = new thread (new test (true ));
Thread t2 = new thread (new test (flase ));
T1.start ();
T2.start ();
}
}

Multi-thread Communication
Routine
--- Input --> resource --- output --->
Thought 1:
Wait (), Y (), and notifyall () are used to operate on

Is it defined in the object class?
1. These methods exist and are being synchronized.
2. When using these methods, you must identify the synchronization lock to which it belongs.
3. The lock can be any object, so any object calls the method.

Must be defined in the object class.

Thinking 2:
What is the difference between wait () and sleep?
Wait (): releases resources and locks.
Sleep (): releases resources without releasing locks.

Inter-thread communication:
In fact, multiple threads are operating on the same resource. However

Different actions.

Class res {
String name;
String sex;
}
Class input implements runnable
{
Private res R;
// Object OBJ = new object ();
Input (res r ){
This. r = R;
}
Public void run (){
Int x = 0;
While (true)
{
// The resource is unique, so data is transferred to R.
Synchronized (r ){
If (x = 0 ){
R. Name = "Mike ";
R. Sex = "man"
} Else {
R. Name = "XX ";
R. Sex = "X"
}
X = (x + 1) % 2;
}
}
}
}
Class output implements runnable
{
Private res R;
// Object OBJ = new object ();
Onput (res r ){
This. r = R;
}
Public void run (){
While (true)
{
Synchronized (r ){
Syso (R. Name + R. Sex );
}
}
}

}

Class inputoutputdemo
{
Public static void main (string []

ARGs)
{
Res r = new res ();

Input in = new input (R );
Output out = new output (R );

Thread T1 = new thread (in );
Thread t2 = new thread (out );

T1.start ();
T2.start ();
}
}

* *** Inter-thread communication -------- waiting for wake-up Mechanism
Example:
Class res {
String name;
String sex;
Boolean flag = false;
}
Class input implements runnable
{
Private res R;
// Object OBJ = new object ();
Input (res r ){
This. r = R;
}
Public void run (){
Int x = 0;
While (true)
{
// The resource is unique, so data is transferred to R.
Synchronized (r ){
If (R. Flag)
Try {R. Wait ();} catch () {} // discard the execution

Qualifications
// Put it in the thread pool
If (x = 0 ){
R. Name = "Mike ";
R. Sex = "man"
} Else {
R. Name = "XX ";
R. Sex = "X"
}
X = (x + 1) % 2;
R. Flag = true;
R. Sort y ();
// Wake up the thread in the thread pool, usually wake up the first wake-up thread
}
}
}
}
Class output implements runnable
{
Private res R;
// Object OBJ = new object ();
Onput (res r ){
This. r = R;
}
Public void run (){
While (true)
{
Synchronized (r ){
If (! R. flay)
Try {R. Wait ();} catch (){}
Syso (R. Name + R. Sex );
R. Flag = false;
R. Sort y ();
}
}
}

}

Class inputoutputdemo
{
Public static void main (string []

ARGs)
{
Res r = new res ();

Input in = new input (R );
Output out = new output (R );

Thread T1 = new thread (in );
Thread t2 = new thread (out );

T1.start ();
T2.start ();
}
}
------------------------------
Wait ():
Notify ():
Policyall ():
All are used in synchronization because you want to pair the line that holds the Monitor (Lock)

.
Therefore, it must be used in synchronization because only synchronization has a lock.

Why are the methods of these operation threads defined in the object class?

?
Because these methods must be identified when operating the synchronization thread

The threads they operate only have locks, and only the same locks are waiting.

Waiting for thread, which can be awakened by the same locks y.
It is not allowed to wake up threads in different locks.

That is to say, waiting and waking must be the same lock.
The lock can be any object, so it can be called by any object.

The method is defined in the object class.

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.