package 线程优先级;
import java.awt.*;
import javax.swing.*;
public class MyThread extends JFrame{
static private Thread thread[]=new Thread[4];
static int k=0;
public MyThread(String title)
{
super(title);
Container container=this.getContentPane();
container.setLayout(null);
JProgressBar pb[]=new JProgressBar[4];
for(int i=0;i<4;i++)
{
pb[i]=new JProgressBar();
pb[i].setStringPainted(true);
}
for(k=0;k<4;k++)
{
thread[k]=new Thread(new Runnable(){
public void run()
{
for(int i=0;i<=100;i++)
{
try{
Thread.sleep(100);
}catch(Exception e)
{
e.printStackTrace();
}
** pb[k].setValue(i);** //这句话似乎是错误的
}
}
}
);
}
thread[0].setPriority(5);
thread[1].setPriority(5);
thread[2].setPriority(4);
thread[3].setPriority(3);
for(int i=0;i<4;i++)
thread[i].start();
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new MyThread("线程优先级");
}
}
错误提示是数组下标越界,我没找到究竟怎么会越界的?求解
我只能看到顺序不对,不知道是不是这个原因,thread[k].sleep(100);这行代码里,thread[k]还没有new出来,没有实例,你写的代码是在new Runnable(){}里,你new这个不就是为了thread[k]本身实例化吗,在实例化的过程中调用,肯定是不对的啊,不过这里应该报空指针吧,下标越界我就不明白了