线程

线程的两种常用创建方式

1.通过继承Thread类,并重写Thread的run()方法

例:
package mutilThread;

/**
 * Created by John on 2017/10/10.
 */
public class FirstThread extends Thread{
    private int i;

    public void run() {
        for(;i<100;i++) {
            System.out.println(getName() + " " + i);
        }
    }
    public static void main(String[] args) {
        for(int i=0;i<100;i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 20) {
                new FirstThread().start();
                new FirstThread().start();
            }
        }
    }
}
注: 通过继承Thread类来创建线程,这种方式不能实现线程资源 共享,即上例中每个FirstThread实例都有各自的i变量。

2.通过实现Runnable接口,并重写该接口的run()方法

package mutilThread;

/**
 * Created by John on 2017/10/10.
 */
public class SecondThread implements Runnable {
    private int i;

    public void run() {
        for(;i<100;i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }

    public static void main(String[] args) {
        for(int i=0;i<100;i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 20) {
                SecondThread st = new SecondThread();
                new Thread(st,"新线程1").start();
                new Thread(st,"新线程2").start();
            }
        }
    }
}

注:上例中,每个Thread共享一个实现了Runnable接口的类(即target对象),即两个Thread共享同一个变量i。

两者区别,通过实现Runnable接口创建线程,多个线程可以共享同一个target对象,从而可以实现线程间的资源共享。

线程控制

1.join控制

join()方法可以让一个线程等待另一个线程完成。当在线程A中调用线程B的join()方法时,线程A将阻塞,直到线程B执行完为止。

例:

package mutilThread;

/**
 * Created by John on 2017/10/10.
 */
public class JoinThread extends Thread{
    public JoinThread(String name) {
        super(name);
    }

    public void run() {
        for(int i=0;i<100;i++) {
            System.out.println(getName() + " " + i);
        }
    }

    public static void main(String[] args) throws Exception{
        new JoinThread("NewThread").start();
        for(int i=0;i<100;i++) {
            if (i == 20) {
                JoinThread jt = new JoinThread("BeingJoinedThread");
                jt.start();
                jt.join();
            }
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }

}
上例中,当主线程执行到i=20时,调用了jt的join()方法,此时main线程被阻塞,知道jt线程执行完后才开始执行main线程,即才开始打印main线程20和以后的数字

2.线程睡眠sleep

通过调用Thread类的静态sleep()方法使得当前线程进入阻塞态,并暂停若干毫秒,当时间到时,线程从阻塞态转为就绪态。

例:

package mutilThread;

import java.util.Date;

/**
 * Created by John on 2017/10/11.
 */
public class SleepTest {
    public static void main(String[] args) throws Exception{
        for(int i=0;i<10;i++) {
            System.out.println("当前时间:" + new Date());
            Thread.sleep(1000);
        }
    }
}
上例中每次打印都会让主线程睡1s。

3.线程让步 yield

同sleep方法,yield也是Thread类的静态方法,调用yield方法不会将该线程阻塞,只是将该线程转入就绪态。即yield方法只是让当前线程暂停一下,让系统的线程调度器重新调度一次。

例:

package mutilThread;

/**
 * Created by John on 2017/10/11.
 */
public class YieldTest extends Thread {
    public YieldTest(String name) {
        super(name);
    }

    public void run() {
        for(int i=0;i<50;i++) {
            System.out.println(getName() + " " + i);
            if (i == 20) {
                Thread.yield();
            }
        }
    }

    public static void main(String[] args) {
        YieldTest yt1 = new YieldTest("高级");
        yt1.start();
        YieldTest yt2 = new YieldTest("低级");
        yt2.start();
    }
}
注:当某个线程调用了yield方法,只有优先级与当前线程相同,或优先级比当前线程更高的处于就绪态的线程才会获得执行的机会。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值