| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 589 人关注过本帖
标题:[求助] 急!急!急!请教一个线程的问题
只看楼主 加入收藏
netfan
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2005-8-15
收藏
 问题点数:0 回复次数:1 
[求助] 急!急!急!请教一个线程的问题
我的源程序如下:
import *;
public class PriorityTest
{
static int numthreads=4;
static boolean yield=true;
static int counter[]=new int[numthreads];
static PrintWriter out =new PrintWriter(System.out,true);
public static void main(String[] args)
{

int numIntervals=10;
if (args.length>0)
{
yield=false;
}
out.println("Using yield()?"+(yield ? "Yes" : "No"));
for(int i=0;i<numthreads;i++)
{


(new PrTest(((i>1)?4 :(i+1)),i)).start();
}
// ThreadInfo.printAllThreadInfo();
out.println();
int step=0;
while(true)
{
boolean alldown=true;
try
{
Thread.sleep(300);

}
catch (InterruptedException e)
{
}
out.print("step "+(step++)+": COUNTERS: ");
for( int j=0;j<numthreads;j++)
{
out.print(" "+counter[j]);
if (counter[j]<2000000)
{
alldown=false;
}
}
out.println();
if (alldown)
{
break;
}
System.exit(0);
}
}

class PrTest extends Thread
{
int id;
PrTest(int priority,int id)
{
super("PrTestThread#"+id);
this.id=id;
setPriority(priority);
}
public void run()
{
for (int i=0;i<=2000000 ;i++ )
{
if ((i % 3000)==0 && PriorityTest.yield)
{
yield();
}
PriorityTest.counter[i]=i;
}
}
}
}

编译提示错误:
PriorityTest.java:21: non-static variable this cannot be referenced from a static context
new PrTest(2,i).start();
^
1 error
请问这是什么原因?
高手帮忙了,
搜索更多相关主题的帖子: 线程 
2005-09-15 09:29
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
import *;
public class PriorityTest
{
  final int NUMTHREADS = 4;
  boolean yield = true;
  PrintWriter pw = new PrintWriter(System.out,true);
  PrTest pt[] = new PrTest[NUMTHREADS];
  
  public void test(int length)
  {
    int numIntervals = 10;
    if(length>0)
    {
      yield=false;
    }
    pw.print("Using yield()?"+(yield ? "Yes" : "No"));
    for(int i=0;i<NUMTHREADS;i++)
    {
      if(i>1)
      {
        pt[i] = new PrTest(this, 4, i);
        pt[i].start();
      }
      else
      {
        pt[i] = new PrTest(this, i+1, i);
        pt[i].start();
      }
    }
    // ThreadInfo.printAllThreadInfo();
    pw.println();
    int step=0;
    while(true)
    {
      boolean alldown=true;
      try
      {
        Thread.sleep(300);
      }
      catch (InterruptedException e)
      {
      }
      pw.print("step "+(step++)+": COUNTERS: ");
      for( int j=0;j<NUMTHREADS;j++)
      {
        pw.println(" " + pt[j].count);
        if (pt[j].count<2000000)
        {
          alldown=false;
        }
      }
      pw.println();
      if (alldown)
      {
        System.exit(0);
      }
    }
  }

  public static void main(String[] args)
  {
    PriorityTest myPriorityTest = new PriorityTest();
    int length = args.length;
    myPriorityTest.test(length);     
  }

  class PrTest extends Thread
  {
    PriorityTest pt;
    int id;
    int count = 0;   
    PrTest(PriorityTest pt, int priority,int id)
    {
      super("PrTestThread#"+id);
      this.pt = pt;
      this.id=id;
      setPriority(priority);
    }
    public void run()
    {
      for(int i=0;i<=2000000 ;i++ )
      {
        if ((i % 3000)==0 && pt.yield)
        {
          yield();
        }
        count = i;
      }
    }
  }
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-16 01:33
快速回复:[求助] 急!急!急!请教一个线程的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020707 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved