这段代码错在哪里

anneyang 2003-08-19 02:48:55
import java.lang.*;

public class Barrier{
private int threads2Wait4;
private InterruptedException iex;

public Barrier(int nThreads)
{
threads2Wait4 = nThreads;
}

public synchronized int waitForRest() throw InterruptedException
{
int threadNum = --threads2Wait4;

if(iex!= null) throw iex;
if(thread2Wait4<=0){
notifyAll();
return threadNum;
}
while(threads2Wait4 > 0){
if(iex!=null) throw iex;
try{
wait();
}catch(InterruptedException ex){
iex = ex;
notifyAll();
}
}
return threadNum;
}

public synchronized void freeAll()
{
iex = new InterruptedException("Barrier Released by freeAll");
notifyAll();
}
}
...全文
38 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wnyu 2003-08-19
  • 打赏
  • 举报
回复
昏,基本间题。
limd 2003-08-19
  • 打赏
  • 举报
回复
public synchronized int waitForRest() throws InterruptedException ///这句改一点
{
zhirenshao 2003-08-19
  • 打赏
  • 举报
回复
throw 应该改为 throws

zhonghua2003 2003-08-19
  • 打赏
  • 举报
回复
搞定了。
whyxx 2003-08-19
  • 打赏
  • 举报
回复
来晚了,up一下
lydong 2003-08-19
  • 打赏
  • 举报
回复
//我kao,伙计,太离谱了吧。
package test;
import java.lang.*;

public class Barrier{
private int threads2Wait4;
private InterruptedException iex;

public Barrier(int nThreads)
{
threads2Wait4 = nThreads;
}

// public synchronized int waitForRest() throw InterruptedException
public synchronized int waitForRest() throws InterruptedException
{
int threadNum = --threads2Wait4;

if(iex!= null) throw iex;
// if(thread2Wait4<=0){
if(threads2Wait4<=0){
notifyAll();
return threadNum;
}
while(threads2Wait4 > 0){
if(iex!=null) throw iex;
try{
wait();
}catch(InterruptedException ex){
iex = ex;
notifyAll();
}
}
return threadNum;
}

public synchronized void freeAll()
{
iex = new InterruptedException("Barrier Released by freeAll");
notifyAll();
}
}
neuboy 2003-08-19
  • 打赏
  • 举报
回复
public synchronized int waitForRest() throw InterruptedException

问题出在 throw 想抛出异常正确写法为 throws

62,614

社区成员

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

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