线程问题:高手求救:

petelhayan 2003-08-25 04:12:59
//选取页面未被处理的页面
public class share_class {
private int page_count;
private int page_id;
private boolean processing=false; //标志以下两个synchronized方法的执行.
boolean[] page_array;
public share_class(int page_count)
{
this.page_count=page_count;
page_array= new boolean[page_count]; //保存页面是否被处理的状态,因为boolean型默认为false,设false 为没被处理过.

}
synchronized int select_page ()
{
while(processing)//finish_page 正在处理,等待
{
try {
wait();
}
catch(InterruptedException e)
{
System.out.println("Exception:"+ e.toString());
}
}
int n;
for (n=0;n<page_count;n++)
{

if (!page_array[n] ) //找出没有被正在处理或已处理过的页
{
page_id = n+1; //因为ppt处理要从1开始;
break;
}
}
processing = true;
notify();

if (page_id> -1)
{
return page_id;

}
else
return -1;//没有页面了就返回 -1;
}
synchronized void finish_page(int page_id)
{
while(!processing)//select_page处理.
{
try
{
wait();
}
catch(InterruptedException e)
{
System.err.println("Exception :" + e.toString());
}
}
page_array[page_id] =true ; //update 对应页面数组位置为已处理过.
processing =false;
notify();

}
}


//处理页面的线程;
public class page_thread implements Runnable{

public page_thread(share_class share,Object pages,Hashtable hash)
{
this.share = share;
this.pages = pages;
this.h1 = hash;

}
public void run()
{
page_id = share.select_page();
share.finish_page(page_id);

if (page_id != -1) {
process page;
}}}

//主程序
....
public static void main(String[] args) {
....
int page_count = 50;
share_class test = new share_class(page_count);
Runnable runnable = new page_thread(test,pages,h1);

Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();

......
}


请大侠帮忙:

问题:1)share_class 我本意是进行同步处理,使线程不会处理同一页面,但是运行结果还是处理同一页面,
2) 本意是让thread1,thread2处理不同页面,两者负责将50页面全部处理完,但是两者都只处理了第一页,其他49页都没有处理,
请大侠帮忙解决同步问题和循环问题,
谢谢先.


...全文
98 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
petelhayan 2003-08-26
  • 打赏
  • 举报
回复
Thanks for your help .
i will study it clearly ,and think it over .
zhirenshao 2003-08-26
  • 打赏
  • 举报
回复
sorry, i must go now, the code I provide is not completely correct, I'll provide another one later time today, probably after 7:00pm
zhirenshao 2003-08-26
  • 打赏
  • 举报
回复
public class Share_class {
private int page_count;
private int page_id;
//private boolean processing=false; //标志以下两个synchronized方法的执行.
boolean[] page_array;
public share_class(int page_count)
{
this.page_count = page_count;
page_array = new boolean[page_count]; /*保存页面是否被处理的状态,因为boolean型默认为false,设false 为没被处理过.*/

}
public synchronized int select_page ()
{
int n;
for (n = 0 ; n < page_count ; n++)
{
if (!page_array[n] ) //找出没有被正在处理或已处理过的页
{
page_id = n + 1; //因为ppt处理要从1开始;
break;
}
}

if (page_id <= page_count) /* if page_id < page_count, it means there are still some pages left*/
return page_id;
else
return -1;//没有页面了就返回 -1;
}

public synchronized void finish_page(int page_id)
{
page_array[page_id] = true ; //update 对应页面数组位置为已处理过.
}
}


public class Page_thread implements Runnable{

Share_class share = null;
Object pages = null;
HashTable h1 = null;

public Page_thread(Share_class share,Object pages,Hashtable hash)
{
this.share = share;
this.pages = pages;
this.h1 = hash;
}
public void run()
{
int page_id = share.select_page();
if (page_id != -1) // if there are more pages
share.finish_page(page_id);

}

public static void main(String[] args) {
....
int page_count = 50;
Share_class test = new Share_class(page_count);
Runnable runnable = new Page_thread(test,pages,h1);

Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
......
}
}

------------------
sorry, i can't type Chinese currently.

You don't have to use 'wait()' and 'notify()', because select_page() and finish_page() is synchronized, so the system will guarantee only one thread can call select_page() and finish_page() at one time.

If a second thread is trying to call select_page() while the first one is still calling, the system will block the second one untill the first thread finish its function and release the lock
CD2 2003-08-26
  • 打赏
  • 举报
回复
你编译通过了吗?
CD2 2003-08-26
  • 打赏
  • 举报
回复
你编译通过了吗?
zhirenshao 2003-08-26
  • 打赏
  • 举报
回复
我上面贴的大致思路应该对的。可能有些细节会有点偏差,不过不影响多线程的操作。
其实只要清楚synchronized的用法就可以了。 上面只用了synchornized的一种用法,synchronized还有另一种用法。 书上都有讲的。 也欢迎交流。 若需要交流,可以给我留言
CD2 2003-08-26
  • 打赏
  • 举报
回复
尽量吧改正思路贴上来,让大家分享。我这两天也在搞多线程。

62,614

社区成员

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

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