线程的建立以及状态

继承Thread类

/*
* 由于子类重写了Thread类的run(), 当调用start()时, 直接找子类的run()方法,不是我们调用的,是虚拟机帮我们调用的,所以我们在查看源码的时候是找不到的
*/

public
class Demo2_Thread { public static void main(String[] args) { MyThread mt = new MyThread(); //4,创建Thread类的子类对象 mt.start(); //5,开启线程,默认调用run方法 for(int i = 0; i < 1000; i++) { System.out.println("bb"); } } } class MyThread extends Thread { //1,继承Thread public void run() { //2,重写run方法 for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中 System.out.println("aaaaaaaaaaaa"); } } }

实现Runnable接口

/*
* 由于子类重写了Thread类的run(), 当调用start()时, 直接找子类的run()方法,不是我们调用的,是虚拟机帮我们调用的,所以我们在查看源码的时候是找不到的
*/

public
class Demo3_Thread { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); //4,创建Runnable的子类对象 //Runnable target = mr; mr = 0x0011 Thread t = new Thread(mr); //5,将其当作参数传递给Thread的构造函数 t.start(); //6,开启线程 for(int i = 0; i < 1000; i++) { System.out.println("bb"); } } } class MyRunnable implements Runnable { //1,定义一个类实现Runnable @Override public void run() { //2,重写run方法 for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中 System.out.println("aaaaaaaaaaaa"); } } }

利用匿名内部类来写线程

public class Demo4_Thread {
    public static void main(String[] args) {       
        new Thread() {                                        //1,继承Thread类
            public void run() {                                //2,重写run方法
                for(int i = 0; i < 1000; i++) {                //3,将要执行的代码写在run方法中
                    System.out.println("aaaaaaaaaaaaaa");
                }
            }
        }.start();                                            //4,开启线程       
        new Thread(new Runnable() {                            //1,将Runnable的子类对象传递给Thread的构造方法
            public void run() {                                //2,重写run方法
                for(int i = 0; i < 1000; i++) {                //3,将要执行的代码写在run方法中
                    System.out.println("bb");
                }
            }
        }).start();                                            //4,开启线程
    }
}

 *********************************线程的状态*********************************************************

山寨版:

官方版(摘自Java Core):

RUNNING:The Java specification does not call this a separate state, though. A running thread is still in the runnable state.

When the thread tries to acquire an intrinsic object lock that is currently held by another thread, it becomes blocked.The thread becomes unblocked when all other threads have relinquished the lock and the thread scheduler has allowed this thread to hold it.(明显官方版的BLOCKED 状态就是山寨版的BLOCKED: SYNCHRONIZED & OTHER)

 When the thread waits for another thread to notify the scheduler of a condition,it enters the waiting state.This happens by calling the Object.wait or Thread.join method,or by waiting for a Lock or Condition in the java.util.concurrent library. In practice,the difference between the blocked and waiting state is not significant.

Several methods have a timeout parameter. Calling them causes the thread to enter the timed waiting state. This state persists either until the timeout expires or the appropriate notification has been received. Methods with timeout include Thread.sleep and the timed versions of Object.wait, Thread.join, Lock.tryLock,and Condition.await.

什邡市

猜你喜欢

转载自www.cnblogs.com/ericguoxiaofeng/p/8547124.html
今日推荐