问一个关于线程同步的小白问题,大家指点一下。

-迷糊- 2011-12-28 07:15:31

public class ThreadA {

public static void main(String[] args) {
ThreadB b = new ThreadB();
b.start();

synchronized (b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {
}
System.out.println("Total is: " + b.total);
}
}
}

class ThreadB extends Thread {
int total;

public void run() {
synchronized (this) {
System.out.println("ThreadB");
for (int i = 0; i < 100; i++) {
total += i;
}
notify();
}
}
}


上面代码里ThreadB中run()的this就是b对象吧?
在b.start();之后一定会先执行主线程里 synchronized (b) {...这段?这是有保证的?
...全文
136 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
-迷糊- 2011-12-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 nmyangym 的回复:]
1 this 就是b.

2 "在b.start();之后一定会先执行主线程里 synchronized (b) {...这段?这是有保证的?"
这是没有保证的。
但是主线程一直在执行,而线程b 需要经过线程调度,才能运行。所以虽无保证,但通常是主线程
“synchronized (b) {...这段”先执行。
如果想看看两个线程同时争抢的情况,可以在 b.s……
[/Quote]
谢谢,说的很明白了~
nmyangym 2011-12-28
  • 打赏
  • 举报
回复
1 this 就是b.

2 "在b.start();之后一定会先执行主线程里 synchronized (b) {...这段?这是有保证的?"
这是没有保证的。
但是主线程一直在执行,而线程b 需要经过线程调度,才能运行。所以虽无保证,但通常是主线程
“synchronized (b) {...这段”先执行。
如果想看看两个线程同时争抢的情况,可以在 b.start();之后,加一短暂(几毫秒)的sleep.
噢噢噢噢 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dy110936 的回复:]

引用 1 楼 aaaa8215 的回复:

synchronized (b) {....}锁住当前线程,等待该线程执行完执行其他线程。

我想问synchronized (b) {....}一定会在run方法之前获得锁吗?
[/Quote]
应该是不一定,但不影响结果
meran 2011-12-28
  • 打赏
  • 举报
回复

/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0 || this != me)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}

private native void start0();



* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
-迷糊- 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aaaa8215 的回复:]

synchronized (b) {....}锁住当前线程,等待该线程执行完执行其他线程。
[/Quote]

我想问synchronized (b) {....}一定会在run方法之前获得锁吗?
aaaa8215 2011-12-28
  • 打赏
  • 举报
回复
synchronized (b) {....}锁住当前线程,等待该线程执行完执行其他线程。

62,614

社区成员

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

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