java 同步块疑惑
自己在学习多线程,synchronized关键字能获取对象的锁,但我测试结果让我还疑惑,同时有2个thread都能进入同步块中,但如果不使用wait、notify,就只能就一个thread进入同步块,请熟悉多线程的牛人帮忙解答下。
public class test {
public static void main(String[] args) {
Object lockObject = new Object();
ThreadT threadT = new ThreadT("thread test1",lockObject);
ThreadT threadW = new ThreadT("thread test2",lockObject);
Thread thread1 = new Thread(threadT);
Thread thread2 = new Thread(threadW);
thread1.start();
thread2.start();
}
}
class ThreadT implements Runnable{
private String name = "";
private Object objectlock = null;
public ThreadT(String name,Object objectlock){
this.name = name;
this.objectlock = objectlock;
}
@Override
public void run() {
synchronized(objectlock){
// TODO Auto-generated method stub
try {
System.out.println(name+" get the lock");
objectlock.wait(1000);
System.out.println(name+" running.......");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
objectlock.notify();
}
}
}
}
输出结果:
thread test1 get the lock
thread test2 get the lock
等待1秒后输出
thread test1 running.......
thread test2 running.......
如果把run方法修改为,
public void run() {
synchronized(objectlock){
// TODO Auto-generated method stub
//try {
System.out.println(name+" get the lock");
// objectlock.wait(3000);
System.out.println(name+" running.......");
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
//}
//finally{
// objectlock.notify();
//}
}
}
执行结果为:
thread test1 get the lock
thread test1 running.......
thread test2 get the lock
thread test2 running.......
public class test {
public static void main(String[] args) {
Object lockObject = new Object();
ThreadT threadT = new ThreadT("thread test1",lockObject);
ThreadT threadW = new ThreadT("thread test2",lockObject);
Thread thread1 = new Thread(threadT);
Thread thread2 = new Thread(threadW);
thread1.start();
thread2.start();
}
}
class ThreadT implements Runnable{
private String name = "";
private Object objectlock = null;
public ThreadT(String name,Object objectlock){
this.name = name;
this.objectlock = objectlock;
}
@Override
public void run() {
synchronized(objectlock){
// TODO Auto-generated method stub
try {
System.out.println(name+" get the lock");
objectlock.wait(1000);
System.out.println(name+" running.......");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
objectlock.notify();
}
}
}
}
输出结果:
thread test1 get the lock
thread test2 get the lock
等待1秒后输出
thread test1 running.......
thread test2 running.......
如果把run方法修改为,
public void run() {
synchronized(objectlock){
// TODO Auto-generated method stub
//try {
System.out.println(name+" get the lock");
// objectlock.wait(3000);
System.out.println(name+" running.......");
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
//}
//finally{
// objectlock.notify();
//}
}
}
执行结果为:
thread test1 get the lock
thread test1 running.......
thread test2 get the lock
thread test2 running.......
...全文
相关推荐
java多线程学习笔记02(csdn)————程序.pdf
java多线程学习笔记02(csdn)————程序
Java 中Object的wait() notify() notifyAll()方法使用
主要介绍了Java 中Object的wait() notify() notifyAll()方法使用的相关资料,需要的朋友可以参考下
java多线程-同步块实例讲解
本文主要介绍java多线程-同步块的知识,这里整理了相关的详细资料及简单示例代码,有兴趣的小伙伴可以参考下
同步块线程的调用--java
在最近的java学习中,写了一些小例子,都是一些常用知识,上传希望大家下载,共同学习。
详解Java利用同步块synchronized()保证并发安全
主要介绍了Java利用同步块synchronized()保证并发安全,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧


Java SE
