Thread thread2 = new Thread()
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()的更多相关文章
- Thread Based Parallelism - Thread in a Subclass
Thread Based Parallelism - Thread in a Subclass 1 import threading import time exit_Flag = 0 class m ...
- Thread系列之Thread.Sleep(0)
线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...
- PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...
- 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 ...
- Mysql thread 与 OS thread
测试环境信息如下: OS:Ubuntu 16.04 LTS Mysql:Mysql 5.7.18,使用docker images运行的实例 Mysql如何处理client请求 在Mysql中,连接管理 ...
- c++并发编程之thread::join()和thread::detach()
thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...
- yum安装提示错误Thread/process failed: Thread died in Berkeley DB library
问题描述: yum 安装更新提示 rpmdb: Thread/process failed: Thread died in Berkeley DB library 问题解决: 01.删除yum临时库文 ...
- 1,Thread 概念以及Thread 的6个状态
Thread 有6个状态 , NEW, RUNNABLE , BLOCKED, WATTING, TIMED WAITING, TERMINATED 1.NEW至今尚未启动的线程的状态.2.RUNNA ...
- Thread之三:Thread Join()的用法
一.join用法 join()和wait()不会释放锁,join()是Thread的方法,wait()是Object的方法 1.join方法定义在Thread类中,则调用者必须是一个线程 例如: Th ...
随机推荐
- SRM 219 Div II Level One: WaiterTipping,小心约分
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...
- Linux下SVN账户密码保存设置
Linux下用SVN进行更新等操作时,总是提示输入用户名和密码,很不方便.因此搜了下解决办法,总结如下: 打开SVN配置文件: vim /home/<user>/.subversion/c ...
- SVN冲突解决详解
在: http://blog.csdn.net/windone0109/article/details/4857044
- 浅谈TCP优化(转)
很多人常常对TCP优化有一种雾里看花的感觉,实际上只要理解了TCP的运行方式就能掀开它的神秘面纱.Ilya Grigorik 在「High Performance Browser Networking ...
- Scroll View 控件以Thumbnail的方式显示一个目录的全部图片,相似图片浏览器
MAC : XCode -> Scroll View 控件以Thumbnail的方式显示一个目录的全部图片,类似图片浏览器 STEP1:将两个目录复制到project里面ImageBrowser ...
- JavaScript类数组对象参考
JavaScript和DOM中有很多类数组对象,它们有以下特点 1.有length属性 2.可以使用[]通过下标访问 3.部分类数组对象使用[]访问成员时不只可以使用下标,还可以使用id或name 4 ...
- 【Android】应用启动画面
几乎所有的Android应用程序都会有一个启动画面,展示自己的LOGO,本版信息,或者更人性化一点的,在很长的加载信息中,变换一些显示的文字等,让无聊的等待时间添加点调味剂. 具体实现来说,应该创建一 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block 到目前为止,我们使用的模块都是在同一个配置 ...
- Leetcode_191_Number of 1 Bits
本文是在学习中的总结.欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44486547 Write a function that ...
- spring问题排查-调低日志等级
问题描写叙述 1. 页面经过一次改动后,提交后页面出现400错误,可是后台并没有输出不论什么错误信息. 2. debug监听应页面对应的提交链接也没有不论什么反应(没有进入后台的controller方 ...