并发编程(1) 线程基本状态和线程的创建

一.线程基本状态

    1.新建状态(new):线程对象创建后将纳入新建状态. Thread t = new Thread();

    2.就绪状态(Runnable):调用start()进入就绪状态

    3.运行状态(running):CPU开始调度处于就绪状态的线程,此线程才真正执行

    4.阻塞状态(Blocked):处于运行时的线程由于某种原因暂时放弃了对CPU的使用权

        (1)等待阻塞:执行wait()方法,当调用wait()时,会把当前锁释放,然后让出CPU,进入等待阻塞状态,当调用notify/notifyAll方法时,会唤醒一个处于等待该对象锁的线程

        (2)同步阻塞:线程在获取synchronized同步锁失败(被其它线程占用)进入同步阻塞状态

        (3)其它阻塞:Thread类的静态方法sleep会阻塞当前线程,在A中调用了B的join方法,表示B执行完才会继续执行A,或发出I/O请求时也会阻塞线程

    5.消亡状态(Dead):线程执行完或因异常推出run方法


二.创建线程的方法

     1.继承Thread类,重写run方法
public class Demo1 extends Thread {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        new Demo1().start();
    }
}

     2.实现runnable接口,实现run方法
public class Demo2 implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }


    public static void main(String[] args) {
        Thread thread = new Thread(new Demo2());
        thread.start();
    }
}

    Demo2不代表一个线程,而是一个线程任务,需要将这个Runnable接口的实现类传递给Thread类,Thread类的run方法执行时首先会检查target是否为空,如果为空就调用Thread类的run方法,上面的1中重写了run方法,所以调用的重写的run方法,这里的2中在new Thread时传入了一个target,所以target不为空,调用的是target的run方法

    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

    3.使用匿名内部类
public class ThreadDemo{


    public static void main(String[] args) {
        new Thread(){
            public void run(){
                System.out.println("sub...");
            }
        }.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("runnable...");
            }
        }).start();
    }
}
    如果写一块(执行结果是sub...)
    
 public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("runnable...");
            }
        }){
            public void run(){
                System.out.println("sub...");
            }
        }.start();
    }
4.带返回值的线程(实现callable接口,实现call方法)
public class callableThread implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("正在进行计算...");
        Thread.sleep(3000);
        return 1;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程任务
        callableThread callableThread = new callableThread();
        FutureTask<Integer> task = new FutureTask<>(callableThread);

        Thread thread = new Thread(task);
        thread.start();
        Integer result = task.get();
        System.out.println("线程执行的结果为:"+result);


    }
}
           实现callable接口,相较于实现runnable接口的方式,方法可以有返回值,并且可以抛出异常
            FultureTask接口继承了Runnable接口和Future接口
            Fulture接口提供了异步计算结果

5.定时器
public class TimerThread {

    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("timer task is run...");
            }
        },0,1000);
    }
}
6.线程池的实现
public class ThreadPoll {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);

        for (int i=0;i<10;i++){
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_34645958/article/details/80930415