加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 07:59:14 所属栏目:Java 来源:网络整理
导读:? ? 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(s

? ?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);            
        }
    }
    
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读