码迷,mamicode.com
首页 > 编程语言 > 详细

多线程的主要操作方法

时间:2015-03-03 06:31:52      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:public   多线程   start   命名   

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();   

         }

}


多线程的主要操作方法

标签:public   多线程   start   命名   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!