⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 priority.java

📁 由于想一次上传成功 所以将二个教程打包成了一个 这些例子几乎包含了所有java的框架集合
💻 JAVA
字号:
class Counter implements Runnable 
{
    Thread thread;
    int counter = 0;
    volatile boolean goflag;

    public Counter(int p) 
    {
        thread = new Thread(this);
        thread.setPriority(p);
    }

    public void start() 
    {
        goflag = true;
        thread.start();
    }

    public void run() 
    {
        while (goflag) counter++;
    }

    public void end() {
        goflag = false;
    }

}

class priority 
{
    public static void main(String args[]) 
    {
        Counter thread1 = new Counter(Thread.NORM_PRIORITY + 2);
        Counter thread2 = new Counter(Thread.NORM_PRIORITY + 1);
        Counter thread3 = new Counter(Thread.NORM_PRIORITY - 1);
        Counter thread4 = new Counter(Thread.NORM_PRIORITY - 2);

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {}

        thread1.end();
        thread2.end();
        thread3.end();
        thread4.end();

        System.out.println("Thread 1 counted: " + thread1.counter); 
        System.out.println("Thread 2 counted: " + thread2.counter);
        System.out.println("Thread 3 counted: " + thread3.counter);
        System.out.println("Thread 4 counted: " + thread4.counter);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -