Thread thread2 = new Thread()
来源:互联网 发布:淘宝个人中心在哪 编辑:程序博客网 时间:2024/05/16 12:24
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 thread2 = new Thread()
- Thread thread = new Thread(method);
- Thread new 的机制
- create new thread
- 抛弃new Thread
- httpclient_demo new thread 线程
- new Thread(t);和new Thread(new ThreadStart(t));
- Thread
- thread
- Thread
- Thread
- thread
- thread
- Thread
- Thread
- Thread
- Thread
- Linux VIM 使用技巧收藏
- 图形/图像外文期刊
- 面试算法(二)实现Singleton模式
- GCD 串行队列,并发队列和主队列的整理
- android单项列表
- Thread thread2 = new Thread()
- Android把手机作为FTP服务器,在PC端管理手机文件的简单实现
- 实例解说Linux中fdisk分区使用方法
- zoj3080chibi
- 基础设施如何支撑海量互联网应用创新
- Thread thread2 = new Thread()
- windows 共享内存
- VS2010/MFC编程入门二(SDK编程和MFC编程框架比较)
- Java包装类的介绍与应用