多线程的学习
多线程
一个进程可以有多个线程
创建线程的方法
-
方法一:
继承Thread类,重写run()方法,调用start开启线程
注意:star方法是多个线程同时进行的
-
方法二:
实现runnable接口,重写run方法,执行线程需要丢入runnable的实现类,调用start方法
-
方法三:


多线程模拟抢票
public class TestThread4 implements Runnable {
private int tickets=10;
@Override
public void run() {
while(true){
if(tickets<=0){
break;
}
try {//模拟延迟
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->拿走了第"+tickets--+"票");
}
}
public static void main(String[] args) {
TestThread4 ticket = new TestThread4();
new Thread(ticket,"小明").start();
new Thread(ticket,"老师").start();
new Thread(ticket,"黄牛党").start();
}
}
结果:

发现:那票的顺序不对,还有就是可能两个人在拿同一张票
线程的五大状态
创建状态,就绪状态,阻塞状态,运行状态,死亡状态

- 线程停止
不要使用JDK已经过时的方法,可能会引起bug,我们可以设计一个变量来控制线程的停止
public class TestStop implements Runnable {
private boolean flag=true;
@Override
public void run() {
int i = 0;
while (flag) {
System.out.println("run..." + i++);
}
}
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if(i==900){
testStop.stop();
System.out.println("线程停止了!");
}
}
}
}
注意:线程启动后,代码可以直接进入下一步,不需要等到线程结束。
- 线程休眠
- sleep(时间)指定当前线程阻塞的毫秒数
- sleep存在异常,要抛出异常
- sleep时间到达后线程进入就绪状态
- sleep可以模拟网络延时,倒计时等
- 每个对象都有一把锁,sleep不会释放锁
public class TestSleep {
public static void main(String[] args) throws InterruptedException {
//tendown(); 倒计时
Date date=new Date(System.currentTimeMillis());//获取当前系统时间
while (true){
System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
Thread.sleep(1000);
date=new Date(System.currentTimeMillis());//更新当前系统时间
}
}
//模拟倒计时
public static void tendown() throws InterruptedException {
int num=10;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if(num<0){
break;
}
}
}
}
- 线程礼让(yield)
- 礼让线程,让当前正在执行的线程暂停,但不阻塞
- 让线程从运行状态转为就绪状态
- 让cpu重新调度,礼让不一定成功,看cpu心情
- 线程强制执行(join)
- Join合并线程,待此线程执行完成后,再执行其他线程
- 可以想象成插队
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 500; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();//启动线程
for (int i = 0; i < 1000; i++) {//主线程
if(i==200){
System.out.println("被插队了!");
thread.join();//插队
}
System.out.println("main线程"+i);
}
}
}
结果:当主线程输出200次以后,线程被插队了,只有vip等到vip线程执行完毕后,主线程才能接着执行。但这个结果能否实现主要还是看cpu。
观测线程状态
调用Thread.State state = thread.getState();方法可以查看当前线程的状态
public class TestState implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("---------");
}
public static void main(String[] args) throws InterruptedException {
TestState testState = new TestState();
Thread thread = new Thread(testState);
Thread.State state = thread.getState();
System.out.println(state);//new
thread.start();//启动线程
state=thread.getState();
System.out.println(state);
while (state!=Thread.State.TERMINATED){
Thread.sleep(100);
state=thread.getState();
System.out.println(state);
}
}
}
注意:线程只能启动一次,线程中断或者结束,一旦进入死亡状态,就不能再次启动。
线程优先级
-
java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行。
-
线程的优先级用数字表示,范围是1~10,最小优先级是1,最大优先级是10.
-
使用一下方式改变或获取优先级
getPriority.setPriority()
public class TestPriority {
public static void main(String[] args) {
Myclass m1 = new Myclass();
Thread t1 = new Thread(m1);
Thread t2 = new Thread(m1);
Thread t3 = new Thread(m1);
Thread t4 = new Thread(m1);
Thread t5 = new Thread(m1);
Thread t6 = new Thread(m1);
//设置优先级在祁东
t1.setPriority(3);
t1.start();
t2.setPriority(6);
t2.start();
t3.setPriority(2);
t3.start();
t4.setPriority(10);
t4.start();
t5.setPriority(8);
t5.start();
t6.setPriority(7);
t6.start();
}
}
class Myclass implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
注意:设置优先级只会在一定程度上决定线程的执行顺序,主要是看cpu如何调度
守护线程(daemon)
- 线程分为离用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true);//上帝守护线程
thread.start();
new Thread(you).start();
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝保佑着你");
}
}
}
//你
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("一生都开心的活着");
}
System.out.println("--------goodbye----------");
}
}
注意:正常情况下上帝这个线程是死循环不会结束的,但是他是守护线程,当用户线程结束后守护线程也会结束。

浙公网安备 33010602011771号