首页 > 编程语言 > 详细

多线程的主要操作方法

时间:2015-03-03 06:28:51      阅读:233      评论:0      收藏:0      [点我收藏+]

1,线程的命名及取得

class MyThreadimplements Runnable{

         @Override

         public void run(){

                   System.out.println(Thread.currentThread().getName());  //线程命名的取得

         }

}

public class TestDemo{

         public static void main(String[] args){

                   MyThread mt = new MyThread();

                   new Thread(mt,"线程A").start(); //线程的命名

                   new Thread(mt,"线程B").start();

                   new Thread(mt,"线程C").start();

                  

         }

}

2、线程的休眠

class MyThreadimplements Runnable{

         @Override

         public void run(){

                   for(int i = 0;i < 50;i++){

                            try {

                                     Thread.sleep(100);   //线程休眠

                            } catch(InterruptedException e) {

                                     e.printStackTrace();

                            }

                   }

                   System.out.println(Thread.currentThread().getName());  //线程命名的取得

         }

}

public classTestDemo {

         public static void main(String[] args){

                   MyThread mt = new MyThread();

                   new Thread(mt,"线程A").start(); //线程的命名

                   new Thread(mt,"线程B").start();

                   new Thread(mt,"线程C").start();

                  

         }

}

3、线程的优先级

取得优先级的时候都是利用一个int整型数据的操作,三种int整型数据的取值:

最高优先级:int MAX_PRIORITY10

中等优先级:int NORM_PRIORITY5

最低优先级:int MIN_PRIORITY1



class MyThreadimplements Runnable{

         @Override

         public void run(){

                   for(int i = 0;i < 50;i++){

                            try {

                                     Thread.sleep(100);   //线程休眠

                            } catch(InterruptedException e) {

                                     e.printStackTrace();

                            }

                   }

                   System.out.println(Thread.currentThread().getName());  //线程命名的取得

         }

}

public classTestDemo {

         public static void main(String[] args){

                   MyThread mt = new MyThread();

                   Thread t1 = newThread(mt,"线程A"); //线程命名

                   Thread t2 = new Thread(mt,"线程B");

                   Thread t3 = newThread(mt,"线程C");

                  

                   t1.setPriority(Thread.MIN_PRIORITY);   //线程优先级顺序

                   t2.setPriority(Thread.MAX_PRIORITY);

                   t3.setPriority(Thread.NORM_PRIORITY);

                  

                   t1.start();  //启动线程

                   t2.start();

                   t3.start();   

         }

}


多线程的主要操作方法

原文:http://9882931.blog.51cto.com/9872931/1616615

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!