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

Java多线程-----Thread常用方法

时间:2018-12-27 00:21:52      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:over   set   com   join()   stack   imp   指定   自己   其它   

   1.public Thread(Runnable target,String name)

创建一个有名称的线程对象

package com.thread.mothed;

public class ThreadMethod {
    public static void main(String[] args) {
        SubThread subThread = new SubThread();
        Thread thread = new Thread(subThread, "线程一");
        thread.start();
    }
}

class SubThread implements Runnable {

    @Override
    public void run() {
        for (int j = 1; j <= 20; j++) {
            System.out.println(Thread.currentThread().getName() + ": " + j);
        }
    }

}

   2.setName(String name)、getName()和currentThread()

  • setName(String name)  设置线程名称 
  • getName() 获取线程名称
  • currentThread() 获取当前线程对象
package com.thread.mothed;

public class ThreadMethod {
    public static void main(String[] args) {
        SubThread subThread = new SubThread();
        Thread thread = new Thread(subThread);
        thread.setName("线程二");
        thread.start();
    }
}

class SubThread implements Runnable {

    @Override
    public void run() {
        for (int j = 1; j <= 20; j++) {
            System.out.println(Thread.currentThread().getName() + ": " + j);
        }
    }

}

   3.join()

把指定的线程加入到当前线程,直到指定线程执行完毕,当前线程才会继续执行

package com.thread.mothed;

public class ThreadMethod {
    public static void main(String[] args) {
       Thread thread=new Thread(new SubThread());    
       thread.start();    
       
       for(int i=1;i<=20;i++) {
           System.out.println(Thread.currentThread().getName()+": "+i);
           if(i==10) {
               try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           }
       }
    }
}

class SubThread implements Runnable{

    @Override
    public void run() {
        for(int j=1;j<=20;j++) {
            System.out.println(Thread.currentThread().getName()+": "+j);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
}

   4.sleep()

使当前正在执行的线程以指定的毫秒数暂停

package com.thread.mothed;

public class ThreadMethod {
    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

   5.yield()

暂停当前正在执行的线程对象,让自己或者其它的线程运行

package com.thread.mothed;

public class ThreadMethod {
    public static void main(String[] args) {
       Thread thread=new Thread(new SubThread());    
       thread.start();    
       
       for(int i=1;i<=50;i++) {
           System.out.println(Thread.currentThread().getName()+": "+i);
           if(i==25) {
               thread.yield();
               System.out.println("-----------------");
           }
       }
    }
}

class SubThread implements Runnable{

    @Override
    public void run() {
        for(int j=1;j<=50;j++) {
            System.out.println(Thread.currentThread().getName()+": "+j);            
        }
    }
    
}

 

Java多线程-----Thread常用方法

标签:over   set   com   join()   stack   imp   指定   自己   其它   

原文地址:https://www.cnblogs.com/fengfuwanliu/p/10166905.html

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