qq46843 Big Liar QQ, I gave him a website, then he does not pay, big liar real record!

Source: Internet
Author: User

qq46843 Big Liar QQ He called Flower total, big liar! I made him a website, and then he didn't pay for it, big liar real record! qq46843 Big Liar QQ, I gave him a website, then he does not pay, big liar real record! There is chat, really do not believe that space is some blowing the information! Really a big liar, big liar QQ He is to do the agent of spinach, the main do Wanda, said there is a proxy he is all liars! Do not believe can add my QQ 45475106 I give you the material to chat! Damn, that's wonderful. The first time by the Spinach agent cheat! We must remember clearly!

Each object is born with a key (monitor), then the method is modified by the synchronized equivalent to a method to add a lock, this method can be synchronized, in multi-threaded time, there is no thread security issues.
The following is a picture to explain:

1. A picture

Pictures can not be seen, please click here: Large HD Map

Write a picture description here

2. Image corresponding to the code


Import Java.util.Date;

/**
* Test the Object class
*
* @author:d Ufy
* @version: 1.0.0
* @date 2017/9/29
* @email [email protected]
*/
public class Objecttest {


Public synchronized void MethodA () {
try {
System.out.println ("This is MethodA ..." + thread.currentthread (). GetName () + ":" + New Date ());
Thread.Sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}

}

public void MethodB () {

System.out.println ("This is MethodB ..." + thread.currentthread (). GetName () + ":" + New Date ());
}

Public synchronized void Methodc () {

try {
System.out.println ("This is methodc ..." + thread.currentthread (). GetName () + ":" + New Date ());
Thread.Sleep (3000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}

}


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Package Com.dufy.concurrentcode;

/**
* Test Thread class
*
* @author:d Ufy
* @version: 1.0.0
* @date 2017/9/29
* @email [email protected]
*/
public class ThreadTest extends thread{

public static void Main (string[] args) {
Objecttest ot = new Objecttest ();
Thread1 T1 = new Thread1 (OT, "thread1");
Thread2 t2 = new Thread2 (OT, "thread2");
Thread3 t3 = new Thread3 (OT, "thread3");
Thread4 T4 = new Thread4 (OT, "thread4");
T1.start ();
T2.start ();
T3.start ();
T4.start ();
}


Static class Thread1 extends thread{
Private Objecttest objecttest;

Public Thread1 (objecttest objecttest,string name) {
SetName (name);
This.objecttest = Objecttest;
}
@Override
public void Run () {
Super.run ();
Objecttest.methoda ();
}
}

Static class Thread2 extends thread{
Private Objecttest objecttest;

Public Thread2 (objecttest objecttest,string name) {
SetName (name);
This.objecttest = Objecttest;
}
@Override
public void Run () {
Super.run ();
Objecttest.methodb ();
}
}

Static class Thread3 extends thread{
Private Objecttest objecttest;

Public Thread3 (objecttest objecttest,string name) {
SetName (name);
This.objecttest = Objecttest;
}
@Override
public void Run () {
Super.run ();
Objecttest.methoda ();
}
}

Static class Thread4 extends thread{
Private Objecttest objecttest;

Public Thread4 (objecttest objecttest,string name) {
SetName (name);
This.objecttest = objecttest;
}
@Override
public void Run () {
Super.run ();
Objecttest.methodc ();
}
}
}
1
2
3
4
5
6
7
8
9

All


+
+
18


+
(
),
,
,
,
,
,
,
,
36
Notoginseng
(
)
All
54
-
+
-
--
-
-
-
--

+
+
/+
(
)
72
All
+
+

+

[
] the "
"
, "
", "the"
, "the" "Bayi
" operation Result:

This is MethodB. Thread2:fri Sep 23:21:17 CST 2017
This is MethodA. Thread1:fri Sep 23:21:17 CST 2017
This is methodc. Thread4:fri Sep 23:21:18 CST 2017
This is MethodA. Thread3:fri Sep 23:21:21 CST 2017
1
2
3
4
Note:
1, the results of the operation may be different from the threading process, there is no relationship, as long as the understanding of the object lock and synchronized core idea is good, the operation of the thread is inherently random this feature.
2, this code is synchronous method, in fact, synchronous code block is also a reason, synchronous code block with synchronized (this) time, when a thread accesses the object of a synchronized (this) synchronization code block, Other threads block access to all other synchronized (this) synchronizations in object (blocking the synchronous block of code and the way the thread can still enter the synchronized code block).

3. Summary

Each object has a lock (object monitor), and the keyword synchronized acquisition lock is an object lock, rather than a piece of code or method (function) as a lock.
Shows which thread first executes the method with the Synchronized keyword and which thread holds the key to the object the method belongs to. Other threads can only be in a wait state.
Call the keyword Synchronized declaration method, must be queued to run. This is the role of using the Synchronized keyword, queued to run, if there is a shared resource, then the read of shared resources is thread-safe.
If a thread holds the key to object, other threads can access the object without a lock method, that is, a non-synchronized type method.
4. Reference

Java multithreaded Programming Core technology


If handsome (beautiful), Wise (Smart), and I as simple and good you see this blog post there are problems, please point out, I humbly accept you let me grow criticism, thank you for reading!
I wish you a happy day!

Welcome to visit my csdn blog, we grow together!

"Whatever you do, just hold on and you'll see something different!" On the way, decently!

qq46843 Big Liar QQ, I gave him a website, then he does not pay, big liar real record!

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.