Java foundation of synchronized

the synchronized
the synchronized keyword is a synchronization lock, action is to let the thread can only be executed one by one, can not be executed in parallel, as to how the underlying implementation, not my kind used in conjunction will not change people's attention.
Usage
- Modified Code fast (non-static)
test code (without the synchronized):
public class Synchronized {
public static void main (String [] args) {
Teacher Teacher = new new Teacher ();
the Thread First = new new SynchronizedThread ( "First", Teacher);
the Thread SECOND = new new SynchronizedThread ( "SECOND", Teacher);
first.start ();
second.start ();
}
static SynchronizedThread the extends the Thread class {
Private String name;
Private Teacher Teacher;
public SynchronizedThread (String name, Teacher Teacher) {
this.name = name;
this.teacher = Teacher; br />}
@Override
RUN void public () {
teacher.say (name);
}
}
static class Teacher {
public void say (name String) {
System.out.println ( "I was teaching" + name);
//System.out.println ( "thread goes to sleep");
SleepUtil.sleep (3000);
System.out.println (name + "sleep three seconds");
}
}
}
The results:
I was teaching first
I was teaching SECOND,
SECOND, sleep three seconds
first three sleep s
two three seconds sleep almost simultaneously.
Test code (plus the synchronized):
static class Teacher {
public void say (name String) {
System.out.println ( "I was teaching" + name);
the synchronized (the this) {
//System.out.println ( "thread into the sleep ");
SleepUtil.sleep (3000);
System.out.println (name +" sleep three seconds ");

}
}
The output:
I was teaching first
I was teaching second
first sleep three seconds
second sleep three seconds
though looking at is the same, when in actual output is to output a sleep three seconds after three seconds, and then outputs a sleep three second.
Summary: a plurality of threads operating in the same object, when the object performs a method of locking, executed one code synchronization code block, lock the object instance
- an object method for modifying
test code (plus the synchronized):
static class Teacher {
public void say the synchronized (name String) {
System.out.println ( "I was teaching" + name);
SleepUtil.sleep (3000);
System.out.println (name + "sleep three seconds");
}
}
The results:
I was taught First
First sleep three seconds
I was teaching SECOND,
SECOND, sleep three seconds
you can see the output is a first thread (first) to perform the method, and then another thread (second) execution methods.
Test code (without the synchronized):
static class Teacher {
public void say (name String) {
System.out.println ( "I was teaching" + name);
SleepUtil.sleep (3000);
System.out.println (name + "sleep three seconds");
}
}
The results:
I was teaching first
I was teaching second
second sleep three seconds
first three seconds of sleep
can be seen almost simultaneously execute two threads, and this block of code and test result is the same.
Test code (a method without synchronized, a method of adding the synchronized):
public class Synchronized {
public static void main (String [] args) {
Teacher Teacher = new new Teacher ();
the Thread First = new new SynchronizedThread ( "First", Teacher) ;
the Thread SECOND = new new AnotherSynchronizedThread ( "SECOND", Teacher);
first.start ();
second.start ();
}
static SynchronizedThread the extends the Thread class {
Private String name;
Private Teacher Teacher;
public SynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;
br/>}
@Override
public void run () {
teacher.say(name);
}
}
static class AnotherSynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public AnotherSynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;
br/>}
@Override
public void run () {
teacher.hello(name);
}
}
static class Teacher {
public synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println (name + "sleep three seconds");
}
public void the Hello (String name) {
System.out.println ( "I would say the Hello");
System.out.println ( "the Hello" + name) ;
}
}
}
the results:
I was teaching first
I would say the Hello
the Hello SECOND,
first sleep three seconds
execution can see, multi-line synchronization thread execution method and asynchronous methods of the same object when the method of phase two affect each other .
Test code (a method of adding synchronized, another method also add the synchronized):
static class Teacher {
public void the synchronized say (name String) {
System.out.println ( "I was teaching" + name);
SleepUtil.sleep (3000) ;
System.out.println (name + "sleep three seconds");
}
public void the synchronized the Hello (String name) {
System.out.println ( "I would say the Hello");
System.out.println ( "
}
}
The results:
I was teaching First
First sleep three seconds
I would say the Hello
the Hello SECOND,
although the results are not apparent output order, but I can see the implementation is to execute a method, then perform another method after the execution.
Test code (a static method without synchronized, an object method plus the synchronized):
public class Synchronized {
public static void main (String [] args) {
Teacher Teacher = new new Teacher ();
the Thread First = new new SynchronizedThread ( "First", Teacher);
the Thread SECOND = new new AnotherSynchronizedThread ( "SECOND", Teacher);
first.start ();
second.start ();
}
static SynchronizedThread the extends the Thread class {
Private String name;
Private Teacher Teacher;
public SynchronizedThread (String name, Teacher Teacher) {
this.name = name;
this.teacher = teacher;
br/>}
@Override
public void run () {
teacher.say(name);
}
}
static class AnotherSynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public AnotherSynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;
br/>}
@Override
public void run () {
Teacher.hello(name);
}
}
static class Teacher {
public synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
static void hello public (String name) {
System.out.println ( "I'll say hello");
System.out.println ( "hello" + name);
}
}
}
The results:
I was teaching first
I'll say hello
SECOND, the Hello
First three seconds of sleep
can be seen from the results, synchronization has no effect on the static method on the object plus method.
Also add a static method synchronized, it does not affect the execution between the two methods.
Summary: 1, first of all static methods plus without synchronization, no impact on an instance method;
2, plus an instance method synchronization, no influence on the instance method without synchronization;
3, are only examples of the method and are added to the synchronous case , the effect will be executed one by one synchronization occurs;
static methods
- static method
above has been known object methods plus synchronization, no effect on the static method.
Test code (two static methods plus the synchronized):
static class Teacher {
public static void the synchronized say (name String) {
System.out.println ( "I was teaching" + name);
SleepUtil.sleep (3000);
System.out .println (name + "sleep three seconds");
}
Public static void the synchronized the Hello (String name) {
System.out.println ( "I would say the Hello");
System.out.println ( "the Hello" + name);
}
}
The results:
I was teaching First
First sleep three seconds
I would say the Hello
the Hello sECOND,
you can see, is the first implementation of a method, then perform another way.
Test code (plus a static method synchronized, a static method without the synchronized):
static class Teacher {
public static void the synchronized say (name String) {
System.out.println ( "I was teaching" + name);
SleepUtil.sleep ( 3000);
System.out.println (name + "sleep three seconds");
}
public static void the Hello (String name) {
System.out.println ( "I would say the Hello");
System.out.println ( "the Hello "+ name);




I would say the Hello
the Hello SECOND,
three seconds first sleep
does not affect the execution of two methods.
Summary: Static methods plus synchronization affects only the addition of static methods XM agent for www.fx61.com/brokerlist/xm.html synchronization.
These are conventional writing, an example method for object instances yoke, yoke static methods to the class. There are other wording.
Other writing
test code (in the example method plus synchronized, locks is the class):
static class Teacher {
public static void say (name String) {
the synchronized (Teacher.class) {
System.out.println ( "I was taught." name +);
SleepUtil.sleep (3000);
System.out.println (name + "sleep three seconds");
}
}
public static void the synchronized the Hello (String name) {
System.out.println ( "I'll say hello" );
System.out.println ( "hello" + name);
}
}
The results:
I was teaching First
First sleep three seconds
I would say hello
hello second
implementation of the results, similar to the addition of two static methods of synchronization, such an approach is locked to the class, all methods require synchronization classes will be affected.
Test code (class defines a variable (an object), is used to synchronize):
public class Synchronized {
Private static new new = the Test Lock the Test ();
public static void main (String [] args) {
Teacher Teacher Teacher new new = () ;
the Thread new new SynchronizedThread = First ( "First", Teacher);
the Thread SECOND = new new AnotherSynchronizedThread ( "SECOND", Teacher);
first.start ();
second.start ();
}
static SynchronizedThread the extends the Thread class {
Private String name;
Teacher Teacher Private;
public SynchronizedThread (String name, Teacher Teacher) {
this.name = name;
this.teacher = Teacher;
br />}
@Override
public void run () {
teacher.say(name);
}
}
static class AnotherSynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public AnotherSynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;
br/>}
@Override
public void run () {
Teacher.hello(name);
}
}
static class Teacher {
public void say(String name) {
synchronized(lock) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
}
public static void hello(String name) {
the synchronized (Lock) {
System.out.println ( "I would say the Hello");
System.out.println ( "the Hello" + name);
}
}
}
static class the Test {
}
}
The results:
I was teaching First
First Sleep three seconds
I would say the Hello
the Hello SECOND,
such an approach is equivalent to the class plus sync, or sync plus examples, but the object synchronization becomes another object's own definition.
synchronized Synchronization:
synchronization instance of an object, this method only impact on the object lock;
synchronization class affect only this class of methods lock;

Guess you like

Origin blog.51cto.com/14511863/2446749