Thread类及其常用方法

32 篇文章 0 订阅
一、Thread类
 

JVM 用来管理线程的一个类,每个线程都有一个唯一的 Thread 对象与之关联,Thread 类的对象就是用来描述一个线程执行流的,JVM 会将这些 Thread 对象组织起来,用于线程调度,线程管理。

二、常见构造方法

方法 
Thread()创建线程对象
Thread(Runnnable target)使用Runnnable对象创建线程对象
Thread(String name)创建线程对象,并命名
Thread(Runnable target,String name)使用Runnnable对象创建线程对象并命名
Thread t1 = new Thread(); 
Thread t2 = new Thread(new MyRunnable()); 
Thread t3 = new Thread("我的线程"); 
Thread t4 = new Thread(new MyRunnable(), "我的线程");

三、创建并启动线程

启动方式:start( ) 方法

创建方法:(1)继承Thread类

public class CreateThreadTest {
    public static void main(String[] args){
        MyThread thread = new MyThread("我的线程");
        thread.start();
    }
}

class MyThread extends Thread{
    public MyThread(String name){
        super(name);
    }
    @Override
    public void run(){
        System.out.println(Thread.currentThread().getName());
    }
}

(2)实现Runnable接口

public class CreateRunnableTest {
    public static void main(String[] args){
        Thread t = new Thread(
                new MyRunnable(),"我的线程");
        t.start();
    }
}

class MyRunnable implements Runnable {
    public MyThread(String name){
        super(name);
    }
    @Override
    public void run(){
        System.out.println("MyRunnable中:"+Thread.currentThread().getName());
    }
}

四、等待一个线程

等待方式:Join( ) 方法

public class JoinTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    for (int i = 0; i < 10; i++) {
                        System.out.println(i);
                        Thread.sleep(100);
                    }
                } catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        });
        t.start();
        //当前线程(代码行所在线程:main)阻塞等待
        //直到T线程执行完后,或者给定时间已经到了(两者最小值)
        //t.join(300000); 0-9-main
        t.join(300); //0-(main)-9
        System.out.println("main");
    }
}

五、获取当前线程引用

获取方式:currentThread( )方法

public class ThreadDemo { 
    public static void main(String[] args) { 、
        Thread thread = Thread.currentThread(); 
        System.out.println(thread.getName()); 
    } 
}

六、休眠当前线程

休眠方式:sleep( )方法

import java.util.Date;

public class SleepTest {
    public static void main(String[] args) throws InterruptedException {
        //日期类:1970-01-01 00:00:00 到当前时间的毫秒数
        // Date data = new Date();
        // long cur = data.getTime();
        // long c = System.currentTimeMillis();
        //Thread.sleep(5000);  作用在主线程
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    for (int i = 0; i <1000000000 ; i++) {
                        System.out.println(i);
                        Thread.sleep(1000);
                        //作用在new的线程
                    }
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });
        //所有非守护线程都结束,进场才结束
        //t.setDaemon(true);
        t.start();
        Thread.sleep(3000);
        System.out.println("main");
    }
}

七、线程串行、并行

public class ThreadAdvantage {
    private static final long NUM = 10_0000_0000L;
    //不是线程越多越好,一般使用CPU的核数来运行,效率比较高
    private static final int COUNT2 = Runtime
            .getRuntime().availableProcessors();
    private static final int COUNT = 2;
    public static void serial() {
        for (int i = 0; i < NUM; i++) {
            i++;
        }
    }

    public static void parallel() {
        for (int i = 0; i < COUNT; i++) {
            new Thread(new Runnable(){
                @Override
                public void run() {
                    serial();
                }
            }).start();
        }
    }

    public static void main(String[] args){
        long start1  =System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            serial();
        }
        long end1 = System.currentTimeMillis();
        System.out.println("串行:"+(end1-start1)+"毫秒");
        long start2  =System.currentTimeMillis();
        parallel();
        //debug方式启动,等待活跃线程数为1,在向下执行
        while(Thread.activeCount()>1){
            Thread.yield();
        }
        long end2 = System.currentTimeMillis();
        System.out.println("并行:"+(end2-start2)+"毫秒");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值