synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制。synchronized关键字用于保护共享数据。

synchronized实现同步的机制:synchronized依靠"锁"机制进行多线程同步,"锁"有2种,一种是对象锁,一种是类锁

1.依靠对象锁锁定

初始化一个对象时,自动有一个对象锁。synchronized {普通方法}依靠对象锁工作,多线程访问synchronized方法,一旦某个进程抢得锁之后,其他的进程只有排队对待。

synchronized {普通方法}依靠对象锁工作,多线程访问synchronized方法,一旦某个进程抢得锁之后,其他的进程只有排队对待。
 
synchronized void method{}功能上,等效于
void method{
   synchronized(this) {
    ...
   }
}
通过代码看比较清楚:

public class TestSynchronized {
public synchronized void method1() throws InterruptedException {
System.out.println("method1 begin at:" + System.currentTimeMillis());
Thread.sleep(6000);
System.out.println("method1 end at:" + System.currentTimeMillis());
}
public synchronized void method2() throws InterruptedException {
while(true) {
System.out.println("method2 running");
Thread.sleep(200);
}
}
static TestSychronized instance = new TestSychronized();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance.method1();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=1; i<4; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 still alive");
}
}
}); Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance.method2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}); thread1.start();
thread2.start(); }
}

 运行结果:thread2一直等到thread1中的method1执行完了之后才执行method2,说明method1和method2互斥

synchronized {修饰代码块}的作用不仅于此,synchronized void method{}整个函数加上synchronized块,效率并不好。在函数内部,可能我们需要同步的只是小部分共享数据,其他数据,可以自由访问,这时候我们可以用 synchronized(表达式){//语句}更加精确的控制
  • 2.synchronized {static方法}此代码块等效于
void method{
   synchronized(Obl.class)
   }
}
使用该类的类对象的锁定去做线程的共享互斥.

package com.free4lab.lol;

public class TestSychronized {
public synchronized static void method1() throws InterruptedException {
System.out.println("method1 begin at:" + System.currentTimeMillis());
Thread.sleep(6000);
System.out.println("method1 end at:" + System.currentTimeMillis());
}
public synchronized static void method2() throws InterruptedException {
while(true) {
System.out.println("method2 running");
Thread.sleep(200);
}
}
static TestSychronized instance1 = new TestSychronized();
static TestSychronized instance2 = new TestSychronized();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance1.method1();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=1; i<4; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 still alive");
}
}
}); Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
instance2.method2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}); thread1.start();
thread2.start(); }
}

输出效果也是method1和method2互斥

 
  • 3.synchronized {run方法}run方法的锁定.
这个举例比较好说

package com.free4lab.lol;

public class TestSychronized {
static TestSychronized instance = new TestSychronized();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public synchronized void run() { for(int i=1; i<4; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 still alive, " + i);
}
}
});
new Thread(thread1).start();
new Thread(thread1).start();
}
}

如果加了synchronized当前线程取完所有数据后,才会释放锁,输出结果是有序的

Thread1 still alive, 1
Thread1 still alive, 2
Thread1 still alive, 3
Thread1 still alive, 1
Thread1 still alive, 2
Thread1 still alive, 3

  

 

最新文章

  1. 如何输出function执行的语句
  2. HTML 表单和输入&lt;textarea&gt;&lt;label&gt;&lt;fieldset&gt;&lt;legend&gt;&lt;select&gt;&lt;optgroup&gt;&lt;option&gt;&lt;button&gt;
  3. 固定IP 正常访问谷歌
  4. sql server service broker中调用存储过程执行跨库操作,不管怎么设置都一直提示 服务器主体 &quot;sa&quot; 无法在当前安全上下文下访问数据库 &quot;dbname&quot;。
  5. 以蓝牙开发的视觉解读微信Airsync协议
  6. 奥威power-BI 在线体验平台
  7. 2016计蒜之道复赛 菜鸟物流的运输网络 网络流EK
  8. Android Studio 将工程作为第三方类库的步骤
  9. c#中从string数组转换到int数组
  10. 检测字节流是否是UTF8编码
  11. dirname(_file_) DIRECTORY_SEPARATOR
  12. openlayers4 入门开发系列之地图属性查询篇(附源码下载)
  13. EF 6.x和EF Core实现返回dynamic类型
  14. Eclipse经常使用快捷键
  15. Ubuntu下使用dialog制作菜单执行简单脚本
  16. 使用VisualSVN Server搭建SVN服务器[xyytit]
  17. HDU5299 圆的扫描线 &amp;&amp; 树上删边博弈
  18. jython
  19. 基于pydpier爬取1药网(转载)
  20. 浅谈Android反调试 之 PTRACE_TRACEME

热门文章

  1. 一个好用的ssh终端:MobaXterm
  2. Java类型简介
  3. Codeforces Round #555 (Div. 3) D. N Problems During K Days 【数学思维】
  4. Android动画及滑动事件冲突解决(转载)
  5. PHP在 win7 64位 旗舰版 报错 Call to undefined function curl_init()
  6. EasyMock set方法报错: java.lang.AssertionError
  7. IO流(二)字符流
  8. WPF中Label使用StringFormat
  9. NetXray
  10. WEB Front-end Development Technology