zoukankan      html  css  js  c++  java
  • 多线程关于优先级。

                                                                        多线程关于优先级

        对多个线程设置不同的优先级

     code:

     package Chapter8;
     class MyThread4 extends Thread
     {
         public void run()
         {
             for(int i=0;i<5;i++)
             {
    System.out.println(i+" "+getName()+"优先级是:"+getPriority());
                 }
         }
     }
    public class ThreadExample3 {
        public static void main(String[] args)
        {
            MyThread4 t1=new MyThread4();
            MyThread4 t2=new MyThread4();
            t1.setPriority(1);
            t2.setPriority(10);
            t1.start();
            t2.start();
            
            
        }

    }
    运行结果:
    0 Thread-0优先级是:1
    0 Thread-1优先级是:10
    1 Thread-1优先级是:10
    2 Thread-1优先级是:10
    3 Thread-1优先级是:10
    4 Thread-1优先级是:10
    1 Thread-0优先级是:1
    2 Thread-0优先级是:1
    3 Thread-0优先级是:1
    4 Thread-0优先级是:1
    照片:

       启动线程的方式有两种,一种是继承Thread 这个类,另一种方式是:实现接口Runnable

    看看下面的这个几行代码有没有什么问题?

    code:

    claass Mythread2 implements Runnable

    {

       public void run()

    {

     for (int i=0;i<5;i++)

    System.out.println(i+" "+getName()+"优先级"+getPriority());

     }

    }

    }

    这几行代码有bug.  Runnable 是一个接口。必须要实现接口的里方法。 而getName()和getPriority()方法是不是这个接口里面的方法

    在Mythread2这个类中也没有getName()方法和getPriority()方法. 必然会报错的。

    在类Thread 中有这几个方法。
    class MyThread4 extends Thread
     {
         public void run()
         {
             for(int i=0;i<5;i++)
             {
    System.out.println(i+" "+getName()+"优先级是:"+getPriority());
                 }
         }
     }


    这才是可以行的通。

    getName()  返回线程的名称

    getPriority()  返回I线程的优先级

    setPriority()  更改线程的优先级

    编程是一门艺术,要爱就要深爱。
  • 相关阅读:
    用Less循环生成样式
    Angular Textarea 高度自动变化
    html文字溢出以省略号(...)替代
    mobiscroll之treelist使用
    安装Microsoft Visual C++ 2010 x64 Redistributable失败的解决方案
    获取input标签的所有属性
    checked
    js中的in-for循环
    checkbox批量选中,获取选中的项的值
    vue 订单列表 多个倒计时
  • 原文地址:https://www.cnblogs.com/pwhit/p/5149004.html
Copyright © 2011-2022 走看看