java synchronized详解

来源:互联网 发布:淘宝儿童化妆品 编辑:程序博客网 时间:2024/06/16 04:57

Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。

1、当两个线程并发访问同一个对象中的某个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行,另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

演示:
package com.demo;


public class DemoSynchronized {


public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
Thread thread=new Thread(test, "ThreadA");
Thread thread2=new Thread(test,"ThreadB");

thread.start();
thread2.start();
}


}


class Test implements Runnable
{


@Override
public void run() {
// TODO Auto-generated method stub
synchronized(this)
{
for(int i=0;i<5;i++)
{
System.out.println(Thread.currentThread().getName()+" synchronized loop "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

2、当一个线程访问对象的synchronized(this)同步代码块时,另一个线程仍然可以访问该对象中的非synchronized(this)同步代码块

演示:

package com.demo;

public class DemoSynchronized {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
Thread thread=new Thread(test, "ThreadA");
Thread thread2=new Thread(test,"ThreadB");

thread.start();
thread2.start();
}




}


class Test implements Runnable
{


@Override
public void run() {
// TODO Auto-generated method stub
String threadName=Thread.currentThread().getName();
if(threadName.equals("ThreadA"))
{
synchronizedMethod();
} else
{
nonsynchronizedMethod();
}
}

public void synchronizedMethod()
{
synchronized (this) {
for(int i=0;i<5;i++)
{
System.out.println("This is the synchronized method.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public void nonsynchronizedMethod()
{
for(int i=0;i<5;i++)
{
System.out.println("This is the nonsynchronized method.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

运行结果如下:

This is the synchronized method.
This is the nonsynchronized method.
This is the synchronized method.
This is the nonsynchronized method.
This is the nonsynchronized method.
This is the synchronized method.
This is the synchronized method.
This is the nonsynchronized method.
This is the synchronized method.
This is the nonsynchronized method.

3、当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞

演示:

package com.demo;




public class DemoSynchronized {




public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
Thread thread=new Thread(test, "ThreadA");
Thread thread2=new Thread(test,"ThreadB");

thread.start();
thread2.start();
}




}


class Test implements Runnable
{


@Override
public void run() {
// TODO Auto-generated method stub
String threadName=Thread.currentThread().getName();
if(threadName.equals("ThreadA"))
{
synchronizedMethod();
} else
{
synchronizedMethod2();
}
}

public void synchronizedMethod()
{
synchronized (this) {
for(int i=0;i<5;i++)
{
System.out.println("This is the synchronizedMethod method.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public void synchronizedMethod2()
{
synchronized (this) {
for(int i=0;i<5;i++)
{
System.out.println("This is the synchronizedMethod2 method.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

运行结果如下:

This is the synchronizedMethod method.
This is the synchronizedMethod method.
This is the synchronizedMethod method.
This is the synchronizedMethod method.
This is the synchronizedMethod method.
This is the synchronizedMethod2 method.
This is the synchronizedMethod2 method.
This is the synchronizedMethod2 method.
This is the synchronizedMethod2 method.
This is the synchronizedMethod2 method.



0 0
原创粉丝点击