线程同步

package com.yhh.chap2;

public class TraditionalThreadSynchronized {

/**

* @param args

*/

public static void main(String[] args) {

new TraditionalThreadSynchronized().init();

}

private void init(){

final Outputer outputer = new Outputer();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output("zhangxiaoxiang");

}

}

}).start();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output2("lihuoming");

}

}

}).start();

}

class Outputer{

public void output(String name){

int len = name.length();

synchronized(this) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

public void output2(String name){

int len = name.length();

synchronized(this) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

}

}

也可以写成以下:


package com.yhh.chap2;

public class TraditionalThreadSynchronized {

/**

* @param args

*/

public static void main(String[] args) {

new TraditionalThreadSynchronized().init();

}

private void init(){

final Outputer outputer = new Outputer();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output("zhangxiaoxiang");

}

}

}).start();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output2("lihuoming");

}

}

}).start();

}

class Outputer{

public void output(String name){

int len = name.length();

synchronized(this) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

//也是代表加的锁是调用此方法的当前对象outputer

public synchronized void output2(String name){

int len = name.length();

// synchronized(this) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

// }

}

}

}

此时上面synchronized(里面代表的是同一个outputer对象)结果正常:

lihuoming

zhangxiaoxiang

zhangxiaoxiang

lihuoming

lihuoming

zhangxiaoxiang

zhangxiaoxiang

lihuoming



package com.yhh.chap2;

public class TraditionalThreadSynchronized {

/**

* @param args

*/

public static void main(String[] args) {

new TraditionalThreadSynchronized().init();

}

private void init(){

final Outputer outputer = new Outputer();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

new Outputer().output("zhangxiaoxiang");

}

}

}).start();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output2("lihuoming");

}

}

}).start();

}

class Outputer{

public void output(String name){

int len = name.length();

synchronized(this) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

public void output2(String name){

int len = name.length();

synchronized(this) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

}

}

此时上面synchronized(里面不是同一个对象,第一个是new了一个新对象,第二个是outputer)

所以结果:出现问题

lihuoming

lihuoming

zhangxiaoxiang

zhangxiaoxlihuoming

iang

lihuoming

zhangxiaoxiang

lihuozhaminngxiaoxig

ang


package com.yhh.chap2;

public class TraditionalThreadSynchronized {

/**

* @param args

*/

public static void main(String[] args) {

new TraditionalThreadSynchronized().init();

}

private void init(){

final Outputer outputer = new Outputer();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output("zhangxiaoxiang");

}

}

}).start();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output2("lihuoming");

}

}

}).start();

}

class Outputer{

public void output(String name){

int len = name.length();

synchronized(Outputer.class) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

public void output2(String name){

int len = name.length();

synchronized(Outputer.class) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

}

}

此时上面调用方法的对象是同一个outputer,synchronized(里面加的是唯一的字节码)

没有问题:

zhangxiaoxiang

lihuoming

lihuoming

zhangxiaoxiang

zhangxiaoxiang

lihuoming

zhangxiaoxiang

lihuoming

zhangxiaoxiang



package com.yhh.chap2;

public class TraditionalThreadSynchronized {

/**

* @param args

*/

public static void main(String[] args) {

new TraditionalThreadSynchronized().init();

}

private void init(){

final Outputer outputer = new Outputer();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

new Outputer().output("zhangxiaoxiang");

}

}

}).start();

new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

outputer.output2("lihuoming");

}

}

}).start();

}

class Outputer{

public void output(String name){

int len = name.length();

synchronized(Outputer.class) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

public void output2(String name){

int len = name.length();

synchronized(Outputer.class) {

for(int i=0;i

System.out.print(name.charAt(i));

}

System.out.println();

}

}

}

}

此时上面调用方法的对象不是同一个,但synchronized(Outputer.class)是一样的所以没得问题:

zhangxiaoxiang

lihuoming

lihuoming

zhangxiaoxiang

zhangxiaoxiang

lihuoming

zhangxiaoxiang

lihuoming

zhangxiaoxiang



你可能感兴趣的:(线程同步)