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

Posted fengfuwanliu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多线程-----Thread常用方法相关的知识,希望对你有一定的参考价值。

   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常用方法的主要内容,如果未能解决你的问题,请参考以下文章

Java:多线程

Java Thread 的 run() 与 start() 的区别

Java多线程中joinyieldsleep方法详解

Java多线程——常用的方法

Java多线程——常用的方法

Java多线程——常用的方法