线程同步问题,来的都给分

xuqq 2004-08-09 03:22:56
问问如下程序中,Synchronized(obj)如何锁的。

程序运行结果如下:
Thread-0 is waitting
Thread-1 is waitting
Thread-2 is will wake up some thread
Thread-0 will wake up
以此结果为例,请前辈讲述过程。




import java.lang.*;
public class notify {
public notify() {
}
public static void main(String[] args) {
Object obj=new Object();
Thread wait1=new Thread(new LockWait(obj));
Thread wait2=new Thread(new LockWait(obj));
Thread notify1=new Thread(new LockNotify(obj));
wait1.start();
wait2.start();
notify1.start();
}
}
class LockWait implements Runnable{
private Object obj;
public LockWait(Object obj){
this.obj=obj;
}
public void run(){
synchronized(obj){
try{
System.out.println(Thread.currentThread().getName()+" is waiting ");
obj.wait();
System.out.println(Thread.currentThread().getName() +" woke up ");
}
catch (InterruptedException e){
}
}
}
}
class LockNotify implements Runnable{
Object obj;
public LockNotify(Object obj){
this.obj=obj;
}
public void run(){
synchronized(obj){
System.out.println(Thread.currentThread().getName()+" will wake up some thread ");
obj.notify();
}
}
}
...全文
317 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
yelang771 2004-08-11
  • 打赏
  • 举报
回复
up
tomcatjava 2004-08-11
  • 打赏
  • 举报
回复
假设开始writeable为true,则getShare等待,setShare可以执行,同时赋值n,然后改写信号量writeable为false,然后通知(notify)getShare获取对象this锁,这是没有等待线程,返回值n。

同理,初始writeable为false,意思是不可写,即不能给n赋值,所以setShare等待,直到得到getShare通知,setShare才可以给n赋值。
xuqq 2004-08-11
  • 打赏
  • 举报
回复
帮我回答完上面这个偶就结贴了
谢谢各位指教!

hfs1978 2004-08-11
  • 打赏
  • 举报
回复
新手,study
xuqq 2004-08-11
  • 打赏
  • 举报
回复

我的程序是商品者,消费者的例子。
Please看下这两段代码:)

synchronized void setShare(int n){
while(!writeable)
try{
wait();
}
catch(InterruptedException e){
}
this.n=n;
writeable=false;
notify();
}
//notify的是要进入setshare 的线程,还是notify包括下面这个getshare 的线程,
我觉得本线程把writable 置为false ,故要进入setshare的线程只能wait 了呀?
矛盾的嘛。
莫非通知了getshare中阻塞的进程?
我主要是看不出来这里面谁是锁旗标:)

synchronized int getShare(){
while(writeable)
try{
wait();
}
catch(InterruptedException e){
}
writeable=true;
notify();
return n;
}

帮我看下嘛。我都发愁了。这么笨,怎么办?
OnlyLikeJava 2004-08-11
  • 打赏
  • 举报
回复
我连问题都看不到!~
miles_z 2004-08-11
  • 打赏
  • 举报
回复
notify后并不意味着放弃锁。而是通知wait线程加入竞争。而notify的那个线程退出sync block,竞争开始。 :)
xuqq 2004-08-10
  • 打赏
  • 举报
回复
这是全部的代码
我想问的是producer的run函数中,setshare()里,writable=true,线程a进来,把writable置为false ,这可以理解,这样别的线程,比如b线成就只能wait .

但是setshare函数里,线程a立刻notify 了,发生了什么事情呢?唤醒b?通过什么?而且a的线程还没完成呢?怎么能唤醒b呢?

谁给我讲讲wait,notify在setshare函数中到底发生了什么事情?谢谢了

//useWaitNotify..Java
public class useWaitNofity {

public static void main(String[] args) {
A a=new A();
producer p=new producer(a);
consumer c=new consumer(a);
p.start();
c.start();
}
}
class producer extends Thread{
private A a;
producer (A a){
this.a=a;
}
public void run(){
for(int i=0;i<5;i++){
try{
Thread.sleep((int)(Math.random()*1000));
}
catch(InterruptedException e){
}
a.setShare(i);
System.out.println(i+"produced by producer.");
}
a.setIsMore(false);
}
}
class A{
private int n=-1;
private boolean isMore=true;
private boolean writeable=true;
synchronized void setShare(int n){
while(!writeable)
try{
wait();
}
catch(InterruptedException e){
}
this.n=n;
writeable=false;
notify();
}
synchronized int getShare(){
while(writeable)
try{
wait();
}
catch(InterruptedException e){
}
writeable=true;
notify();
return n;
}
boolean isAnyMore(){
return isMore;
}
void setIsMore(boolean b){
isMore=b;
}
}
class consumer extends Thread{
private A a;
consumer(A a){
this.a=a;
}
public void run(){
while(a.isAnyMore()){
try{
Thread.sleep((int)(Math.random()*1000));
}
catch(InterruptedException e){
}
System.out.println(a.getShare()+"consumed by consumer");
}
}
}
xuqq 2004-08-10
  • 打赏
  • 举报
回复
如果同步的是一个方法
那么他的锁旗标是什么呢?
比如下面这个例子。
讲祥细点好吗?

synchronized void setShare(int n){
while(!writeable)
try{
wait();
}
catch(InterruptedException e){
}
this.n=n;
writeable=false;
notify();
}
xuqq 2004-08-10
  • 打赏
  • 举报
回复
那么这个object旗标开始是0啊,如何进得了同步块呢
必须要object为1时,才能进同步块的,对吗?
miles_z 2004-08-10
  • 打赏
  • 举报
回复
这里楼主可能认为,thread-0 wait的时候,还没有释放obj;而这个obj也是thread-1的lock,所以thread-1无法进入sync block。其实问题正出在这个假设上。

看看wait的javadoc:

* The current thread must own this object's monitor. The thread
* releases ownership of this monitor and waits until another thread
* notifies threads waiting on this object's monitor to wake up
* either through a call to the <code>notify</code> method or the
* <code>notifyAll</code> method. The thread then waits until it can
* re-obtain ownership of the monitor and resumes execution.

看到么?

。。。 The thread releases ownership of this monitor 。。。

如果你这里把wait换成为sleep,就完全不同了。要勤看文档,呵呵。
haode 2004-08-10
  • 打赏
  • 举报
回复
http://www.meetchinese.com/earticles/xr_html/articles/63/306.html

这篇文章可能对你有帮助
xuqq 2004-08-10
  • 打赏
  • 举报
回复
你的意思是notify后并不意味着放弃了 锁,是吗?
等该进程完了才放弃是吗?
:)
xuqq 2004-08-10
  • 打赏
  • 举报
回复
可是object开始是0啊,怎么进同步块的啊?
:)
ITxiaopang 2004-08-10
  • 打赏
  • 举报
回复
UP
antingluo 2004-08-10
  • 打赏
  • 举报
回复
up
miles_z 2004-08-10
  • 打赏
  • 举报
回复
代码太长...

synchronize方法,用this作锁。也就相当于:

void setShare(int n){
synchronized(this) {
while(!writeable)
try{
wait();
}
catch(InterruptedException e){
}
this.n=n;
writeable=false;
notify();
}
}

notify:

Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

关键在于第二段:
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.

明白了么?多看文档阿 :)
tiansilai 2004-08-10
  • 打赏
  • 举报
回复
学习
xuqq 2004-08-10
  • 打赏
  • 举报
回复
那位如果知道的话,请帮忙回答我上面的问题。再不结贴恐怕夭折了。

谢过了!

仅仅100分,大家分了。请大家帮忙。
sunyonglinbj 2004-08-10
  • 打赏
  • 举报
回复
我也不懂,学习,AND UP!!!
加载更多回复(17)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧