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 ...
随机推荐
- C++ STL源代码学习之算法篇
///因为篇幅太长,因此,删去了非常多接口,仅仅分析了内部实现,算法对迭代器的要求也被删去 /// search. template <class _ForwardIter1, class _F ...
- 让Linux开机运行命令
开机的时候需要linux 自动执行命令很简单 只需要把要执行的命令输入操作系统启动的时候要加载的文件里面就行了,一般写在 /etc/rc.local里面 #vim /etc/rc.local 按o键 ...
- PDF数据防扩散系统方案
在企业信息化过程中.大量的企业重要图纸和资料都是以电子文件的方式存在.为了避免内部关键数据的外泄,採取了多种方式:设计部门的门禁管制.防火墙.禁止计算机的USB接口等等. 可是泄密问题还是时有发生,原 ...
- freemarker导出word带图片
导出word带图片 如果你需要在word中添加图片,那你就在第一步制作模板时,加入一张图片占位,然后打开xml文档,可以看到如下的一片base64编码后的代码: <w:binData w:nam ...
- WPF实现无窗体鼠标跟随
原文:WPF实现无窗体鼠标跟随 上次的弹力模拟动画实现后,我觉得可以把这个弄得更好玩一些,我们可以让小球实时跟随着鼠标,并且还可以让窗口完全消失,让小球在桌面上飞来飞去. 这只需要一些简单的修改就可以 ...
- Matlab hermite
保形分段三次hermite插值 % 这是MATLAB里面的pchip.m文件.这里把它的凝视改写成汉语,主要是想弄清楚它是怎么计算在节点处的导数的. function v = pchip(x,y,xx ...
- Android开发之Buidler模式初探结合AlertDialog.Builder解说
什么是Buidler模式呢?就是将一个复杂对象的构建与它的表示分离,使得相同的构建过程能够创建不同的表示.Builder模式是一步一步创建一个复杂的对象,它同意用户能够仅仅通过指定复杂对象 ...
- swift 学习资源 大集合
今天看到一个swift学习网站,其中我们收集了大量的学习资源 Swift 介绍 Swift 介绍 来自 Apple 官方 Swift 简单介绍 (@peng_gong) 一篇不错的中文简单介绍 [译] ...
- sql server事物控制
一.多个数据库 1.存储过程 2.Commit写在 Try...Catch后面 protected void Button1_Click(object sender, EventArgs e) ...
- 【iOS开发-60】案例学习:多组数据的tableView设置、添加右側组索引、多层数据模型设置以及valueForKeyPath
效果: 这里的数据模型有两层:每一组汽车是一层模型,每一组里面的每一行汽车品牌也是一层模型. (1)我们先创建一个WSCars模型. 在WSCars.h中: #import <Foundatio ...