线程的建立以及状态

继承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,开启线程
    }
}

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

RUNNABLE:当需要新起一个线程来执行某个子任务时,就创建了一个线程。但是线程创建之后,不会立即进入就绪状态,因为线程的运行需要一些条件(比如内存资源,在前面的JVM内存区域划分一篇博文中知道程序计数器、Java栈、本地方法栈都是线程私有的,所以需要为线程分配一定的内存空间),只有线程运行需要的所有条件满足了,才进入就绪状态。

RUNNING:当线程进入就绪状态后,不代表立刻就能获取CPU执行时间,也许此时CPU正在执行其他的事情,因此它要等待。当得到CPU执行时间之后,线程便真正进入运行状态。

摘自:Java并发编程:Thread类的使用   感谢

山寨版:

官方版(摘自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.

 

 

 

什邡市

转载于:https://www.cnblogs.com/ericguoxiaofeng/p/8547124.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值