java 线程 join start,java多线程源码解读 start,run,join,yied

java

/**

Created by 陈奇 on 2017/4/14 0014.

*/

public class ThreadHelloWord implements Runnable {

@Override

public void run() {

System.out.println("hello word");

}

public static void main(String[] args) {

ThreadHelloWord t = new ThreadHelloWord();

Thread th = new Thread(t);

System.out.println("states="+th.getState());

th.start();

System.out.println(th.getName());

System.out.println("states="+th.getState());

}

}

* 这一段代码创建了一个线程,并打印出hello Word,

先看一下runnable接口源码吧.

```java```

/* @author Arthur van Hoff

* @see java.lang.Thread

* @see java.util.concurrent.Callable

* @since JDK1.0

*/

@FunctionalInterface

public interface Runnable {

/**

* When an object implementing interface Runnable is used

* to create a thread, starting the thread causes the object's

* run method to be called in that separately executing

* thread.

*

* The general contract of the method run is that it may

* take any action whatsoever.

*

* @see java.lang.Thread#run()

*/

public abstract void run();

} ```

* Runnable自jdk1.0定义,只定义了一个run方法。实现接口必须实现其方法,所以重写了run方法。

在看下Thread类

```java```

public

class Thread implements Runnable {

/* Make sure registerNatives is the first thing does. */

private static native void registerNatives();

static {

registerNatives();

}

Thread类实现了runnable接口,并获取寄存器,也就是说每一个线程都有一个寄存器,再看下star()方法

java

public synchronized void start() {

/**

* This method is not invoked for the main method thread or "system"

* group threads created/set up by the VM. Any new functionality added

* to this method in the future may have to also be added to the VM.

*

* A zero status value corresponds to state "NEW".

*/

if (threadStatus != 0)

throw new IllegalThreadStateException();

/* Notify the group that this thread is about to be started

* so that it can be added to the group's list of threads

* and the group's unstarted count can be decremented. */

group.add(this);

boolean started = false;

try {

start0(); //调用本地方法

started = true;

} finally {

try {

if (!started) {

group.threadStartFailed(this);

}

} catch (Throwable ignore) {

/* do nothing. If start0 threw a Throwable then

it will be passed up the call stack */

}

}

}

private native void start0(); //本地方法执行线程

这里有一个小疑问,既然线程有自己独立的寄存器为什么还要加锁呢?

yield()方法让出执行权

```java```

public static native void yield(); //本地方法

package com.cn.threadpool;

/**

* Created by 陈奇 on 2017/4/14 0014.

*/

public class ThreadHelloWord extends Thread {

private String helloword;

ThreadHelloWord(){}

ThreadHelloWord(String helloword){

super(helloword);

}

@Override

public void run() {

for (int i = 0; i <20 ; i++) {

System.out.println("" + this.getName() + "-----" + i);

// 当i为10时,该线程就会把CPU时间让掉,让其他或者自己的线程执行(也就是谁先抢到谁执行)

if (i == 10) {

this.yield();

}

}

}

public static void main(String[] args) {

ThreadHelloWord h = new ThreadHelloWord("h");

ThreadHelloWord w = new ThreadHelloWord("w");

Thread th = new Thread(h);

Thread tw = new Thread(w);

th.start();

tw.start();

}

}

539a130efe88

Paste_Image.png

539a130efe88

Paste_Image.png

join() 方法

源码

java

public final void join() throws InterruptedException {

join(0);

}

public final synchronized void join(long millis)

throws InterruptedException {

long base = System.currentTimeMillis();

long now = 0;

if (millis < 0) {

throw new IllegalArgumentException("timeout value is negative");

}

if (millis == 0) {

while (isAlive()) {

wait(0);

}

} else {

while (isAlive()) {

long delay = millis - now;

if (delay <= 0) {

break;

}

wait(delay); // 调用wait方法

now = System.currentTimeMillis() - base;

}

}

}

join方法调用的是object的wait方法,

demo

```java```

/**

* Created by 陈奇 on 2017/4/16 0016.

*/

public class JoinTest extends Thread {

JoinTest(String name){

super(name);

}

@Override

public void run() {

try {

sleep(30);

System.out.println(this.getName()+"____run ");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws InterruptedException {

Thread t = new Thread(new JoinTest("t"));

Thread t1 = new Thread(new JoinTest("t1"));

t.start(); // wait(0),立即执行

t.join();

t1.start();

t1.join(10); //main线程等待时间

System.out.println("main end");

}

}

539a130efe88

Paste_Image.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值