java synchronized详解
来源:互联网 发布:淘宝儿童化妆品 编辑:程序博客网 时间:2024/06/16 04:57
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。
1、当两个线程并发访问同一个对象中的某个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行,另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。
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 synchronizedMethod2 method.
This is the synchronizedMethod2 method.
This is the synchronizedMethod2 method.
This is the synchronizedMethod2 method.
This is the synchronizedMethod2 method.
- java synchronized详解--synchronized方法
- java synchronized详解java synchronized详解
- [Java] Java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解(一)
- java synchronized详解(二)
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- java synchronized详解
- mvn出现GC overhead limit exceeded
- Android架构设计模式总结(MVX)
- logstash elasticsearch kibana搭建日志平台
- 策略模式
- 【Unity&人物属性】人物属性大量设置用set,get方法还是public成员
- java synchronized详解
- ES6学习笔记之《let 和 const》
- 基于改进的RPCA人脸识别算法
- 贪婪与懒惰--正则表达式
- STM32 RTC
- String StringBuffer StringBuilder
- 【Oracle】RAC11gR2 Grid启动顺序及启动故障诊断思路
- link js重构心得
- Java 多线程