Thread thread2 = new Thread() {

  @Override

  public void run() {

  test.function();

  }

  };

  thread1.start();

  thread2.start();

  }

  }

  class TestCase {

  public synchronized void function() {// add synchronized keyword.

  for (int i = 0; i < 5; i++) {

  System.out.println(Thread.currentThread()。getName() + " executed result: " + i);

  }

  }

  }

  执行程序。得到的输出结果总是例如以下:

  Thread-0 executed result: 0

  Thread-0 executed result: 1

  Thread-0 executed result: 2

  Thread-0 executed result: 3

  Thread-0 executed result: 4

  Thread-1 executed result: 0

  Thread-1 executed result: 1

  Thread-1 executed result: 2

  Thread-1 executed result: 3

  Thread-1 executed result: 4

  从输出结果能够看出,同步了方法之后,两个线程顺序输出。说明线程Thread-1进入function方法后,线程Thread-2在方法外等待,等Thread-1运行完后释放锁,Thread-2才进入方法运行。

  可是我们对上面的代码稍作改动。看看同步方法。对于不同的对象情况下是否都有线程同步的效果。

  [測试程序2.2]

  /**

  * Test case 2.2. synchronized method but different objects.

  */

  public class Test {

  public static void main(String[] args) {

  // There are two objects.

  final TestCase test1 = new TestCase();

  final TestCase test2 = new TestCase();

  Thread thread1 = new Thread() {

  @Override

  public void run() {

  test1.function();

  }

  };

  Thread thread2 = new Thread() {

  @Override

  public void run() {

  test2.function();

  }

  };

  thread1.start();

  thread2.start();

  }

  }

  class TestCase {

  public synchronized void function() {// add synchronized keyword.

  for (int i = 0; i < 5; i++) {

  System.out.println(Thread.currentThread()。getName() + " executed result: " + i);

  }

  }

  }

  执行程序,某次的执行结果例如以下:

  Thread-0 executed result: 0

  Thread-0 executed result: 1

  Thread-1 executed result: 0

  Thread-1 executed result: 1

  Thread-0 executed result: 2

  Thread-0 executed result: 3

  Thread-0 executed result: 4

  Thread-1 executed result: 2

  Thread-1 executed result: 3

  Thread-1 executed result: 4

  从以上结果能够看出,同步方法时,当一个线程进入一个对象的这个同步方法时。还有一个线程能够进入这个类的别的对象的同一个同步方法。

  同步方法小结

  在多线程中,同步方法时:

  同步方法,属于对象锁,仅仅是对一个对象上锁;

  一个线程进入这个对象的同步方法,其它线程则进不去这个对象全部被同步的方法。能够进入这个对象未被同步的其它方法;

  一个线程进入这个对象的同步方法,其它线程能够进入同一个类的其它对象的全部方法(包含被同步的方法)。

  同步方法仅仅对单个对象实用。

  对静态方法的同步

  上面是对普通的方法进行同步,发现仅仅能锁对象。那么这次我们试着同步静态方法看会有什么结果。与上面的[測试程序2.2]来进行对照。

  [測试程序3.1]

  /**

  * Test case 3.1. synchronized static method.

  */

  public class Test {

  public static void main(String[] args) {

  // There are two objects.

  final TestCase test1 = new TestCase();

  final TestCase test2 = new TestCase();

  Thread thread1 = new Thread() {

  @Override

  public void run() {

  test1.function();

  }

  };

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Thread thread2 = new Thread()的更多相关文章

  1. Thread Based Parallelism - Thread in a Subclass

    Thread Based Parallelism - Thread in a Subclass 1 import threading import time exit_Flag = 0 class m ...

  2. Thread系列之Thread.Sleep(0)

    线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...

  3. PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别

    链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...

  4. Part 92 Significance of Thread Join and Thread IsAlive functions

    Thread.Join & Thread.IsAlive functions Join blocks the current thread and makes it wait until th ...

  5. Mysql thread 与 OS thread

    测试环境信息如下: OS:Ubuntu 16.04 LTS Mysql:Mysql 5.7.18,使用docker images运行的实例 Mysql如何处理client请求 在Mysql中,连接管理 ...

  6. c++并发编程之thread::join()和thread::detach()

    thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...

  7. yum安装提示错误Thread/process failed: Thread died in Berkeley DB library

    问题描述: yum 安装更新提示 rpmdb: Thread/process failed: Thread died in Berkeley DB library 问题解决: 01.删除yum临时库文 ...

  8. 1,Thread 概念以及Thread 的6个状态

    Thread 有6个状态 , NEW, RUNNABLE , BLOCKED, WATTING, TIMED WAITING, TERMINATED 1.NEW至今尚未启动的线程的状态.2.RUNNA ...

  9. Thread之三:Thread Join()的用法

    一.join用法 join()和wait()不会释放锁,join()是Thread的方法,wait()是Object的方法 1.join方法定义在Thread类中,则调用者必须是一个线程 例如: Th ...

随机推荐

  1. Mvc 异常处理 ajax的 和 不是ajax的!

    using ImageUpload.Auth; using System; using System.Collections.Generic; using System.Linq; using Sys ...

  2. 天嵌E9平台下进行USB Wifi模块RT3070驱动的移植

    因为项目工作须要,要在天嵌E9平台下实现wifi的点对点,点对多点的传输. Wifi 模块芯片:雷凌3070芯片 嵌入式平台:E9(ARM) 交叉编译环境:arm-none-linux-gnueabi ...

  3. SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理

    原文:SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理 SQL Server 字段类型 decimal(18,6)小数点前是几位? 不可否认,这是 ...

  4. Redhat 6.3中syslog信息丢失

    我们採用Linux的syslog来记录产品的debug log. 调用当中的一个可运行文件.运行完命令之后,查看debug log的信息,竟然从某一条log之后的log都丢失了.多次尝试后,发现每次都 ...

  5. Windows Phone开发(37):动画之ColorAnimation

    原文:Windows Phone开发(37):动画之ColorAnimation 上一节中我们讨论了用double值进行动画处理,我们知道动画是有很多种的,今天,我向大家继续介绍一个动画类--Colo ...

  6. 在Amazon AWS RHEL 7上安装 配置PPTP VPN

    0 前言 0.1 为什么需要VPN? 国内的VPN不是必须,但是国外的VPN是很有用的.连接到国外的VPN服务器之后就可以访问Google,Facebook, Youtube等网站,没有Google的 ...

  7. C++转让Lua

    转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/39756423 我使用的cocos2d-x版本号是2.3.3.先在一个C++projec ...

  8. vultr centos x64 6.5.x 升级php7.0

    升级前,先卸载 php5.6.x 卸载php5.6.2 从cent 6.5.x 需要卸载: yum remove php56u-mysqlnd-5.6.20-1.ius.centos6.x86_64 ...

  9. checkbox的attr(&quot;checked&quot;)一直以来,undefined问题解决

    最近,屌丝要项目开发的需要,需要一个完整的选checkbox特征. 该死的~~这不是很easy什么东西,共checkbox,N多个子的checkbox,总checkbox一旦选定,儿checkbox所 ...

  10. Android的第二次增加SurfaceView基本使用

    本文来源于http://blog.csdn.net/hellogv/ ,引用必须注明出处. 上次介绍MediaPlayer的时候略微介绍了SurfaceView,SurfaceView因为能够直接从内 ...