欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java线程优先级示例代码

程序员文章站 2023-12-20 17:32:10
使用过bit下载软件的同学应该很清楚,我们有多个下载任务同时执行,而其中的某一个或多个是非常重要的,于是给这些任务设定一个高度优先,以便任务可以获取更多的带宽尽早完成下载。...

使用过bit下载软件的同学应该很清楚,我们有多个下载任务同时执行,而其中的某一个或多个是非常重要的,于是给这些任务设定一个高度优先,以便任务可以获取更多的带宽尽早完成下载。java线程的优先级也差不多,优先级越高排程器就会给它越多的cpu执行时间,但请注意:如果有多个线程在等待一个机锁的时候,并不是优先级越高就可以越早执行。

复制代码 代码如下:

import java.awt.borderlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.jtextfield;

/**
 * 线程的优先级
 * 10个计数器线程分别被设置了不同的优先级,我们通过计数器的累加来观察优先级的作用
 * @author 五斗米
 * @blog http://blog.csdn.net/mq612
 */
public class testmain extends jframe {
    private mythread [] thread = null; // 要操作的线程
    private jpanel pane = null;
    private jbutton startbutton = null, stopbutton = null; // 启动、结束按钮

    public testmain(){
        super("线程的优先级");
        pane = new jpanel();
        thread = new mythread[10];
        for(int i = 0; i < 10; i++){ // 线程的优先级最小是1,最大是10
            thread[i] = new mythread(i + 1);
        }
        startbutton = new jbutton("执行");
        startbutton.addactionlistener(new actionlistener(){
            public void actionperformed(actionevent e) {
                for(int i = 0; i < 10; i++){
                    thread[i].start();
                }
            }
        });
        stopbutton = new jbutton("结束");
        stopbutton.addactionlistener(new actionlistener(){
            public void actionperformed(actionevent e) {
                for(int i = 0; i < 10; i++){
                    thread[i].quit();
                }
            }
        });
        jpanel p = new jpanel();
        p.add(startbutton);
        p.add(stopbutton);
        this.getcontentpane().add(pane);
        this.getcontentpane().add(p, borderlayout.north);
        this.setdefaultcloseoperation(jframe.exit_on_close);
        this.setsize(500, 300);
        this.setlocationrelativeto(null);
        this.setvisible(true);
    }
    /**
     * 计数器线程
     */
    class mythread extends thread{
        private jtextfield text = null; // 计数器
        private int i = 0; // 计数器
        private int priority = 0; // 优先级
        private jlabel label = null; // 优先级显示标签
        private boolean b = true; // 控制线程结束的boolean变量

        public mythread(int priority){
            this.priority = priority;
            this.setpriority(priority);
            jpanel p = new jpanel();
            label = new jlabel("priority=" + priority);
            text = new jtextfield(12);
            p.add(label);
            p.add(text);
            pane.add(p); // 将自己的计数器加入主窗口面板中
        }
        /**
         * 结束线程
         */
        public void quit(){
            b = false;
        }
        public void run(){
            while(b){
                this.text.settext(integer.tostring(i++));
                try {
                    this.sleep(1); // 减小这里的毫秒数,可以让我们更容易观察到结果
                } catch (interruptedexception ex) {
                    ex.printstacktrace();
                }
            }
        }
    }

    public static void main(string [] args){
        new testmain();
    }  

上一篇:

下一篇: