在java设置线程优先级的方法
本实例定义4设置进度条进度4线程,setpriority()方法,在这种情况下定义,设置每个线程的优先级和名称,等等。这种情况下运行时,第一个看起来像滚在一起。如果你仔细看,有细微的差别。每次滚动都是第一次改变。线程优先级方法的示例如下所示:
进口java AWT *;
进口javax.摆动。*;
公共课prioritytest扩展JFrame {
私有静态最终长1l serialVersionUID =;
专用线程开启;
专用线程threadb;
专用线程threadc;
专用线程threadd;
公共prioritytest(){
GetContentPane (.SetLayout (New) GridLayout (4,1)); / / set the container for the grid layout.
最后jprogressbar ProgressBar =新jprogressbar();
最后jprogressbar progressbar2 =新jprogressbar();
最后jprogressbar progressbar3 =新jprogressbar();
最后jprogressbar progressbar4 =新jprogressbar();
GetContentPane()加(条);
GetContentPane()加(progressbar2);
GetContentPane()加(progressbar3);
GetContentPane()加(progressbar4);
progressbar.setstringpainted(真的);
progressbar2.setstringpainted(真的);
progressbar3.setstringpainted(真的);
progressbar4.setstringpainted(真的);
4分别实例化过程。
开启=新线程(新必须(ProgressBar));
threadb =新线程(新必须(progressbar2));
threadc =新线程(新必须(progressbar3));
threadd =新线程(新必须(progressbar4));
设置线程优先级。
setpriority(开启
setpriority(threadb
setpriority(threadc
setpriority(threadd
}
名称,设置线程优先级方法。
public static void setpriority(字符串threadname,中断优先级,线程T){
T.setPriority(优先);
T.setName(threadname);
T.start();
}
显示窗口。
public static void main(String { } args){
init(新prioritytest(),100100);
}
显示方法设置窗口。
public static void init(JFrame框架,宽度,高度深度){
frame.setdefaultcloseoperation(JFrame。exit_on_close);
指定框架窗口的大小(宽度,高度);
frame.setvisible(真的);
}
/ /定义一个类实现Runnable接口。
私人最终的类必须实现Runnable {
私人最终jprogressbar酒吧;
int计数= 0;
个人必须(jprogressbar吧){
这个;
}
公共空运行(){
当(真){
Bar.setValue(计数+ = 10);
尝试{
(1000)threada.sleep;
} catch(InterruptedException e){
System.out.println(当前线程被中断!;
}
}
}
}
}
运行结果图: