目录

实现多线程,有两种方法

  1. 继承thread,重写run方法,【本质也是实现runnable接口的一个实例】(比较拿捏)
  2. 创建一个类,implements runnable接口【这个好处多,建议用这个】

方法1:

package xiancheng.MyThread;

/**
* Created with IntelliJ IDEA.
*
* @Author: 从南到北
* @Date: 11/29/2021/11:10
* @Description:
* 线程拿捏的方法:
*
*/
public class Mythread extends Thread {
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(i);
}
}

public static void main(String[] args) {
Mythread m1 = new Mythread();
Mythread m2 = new Mythread();

// 这样不会启动线程,只是单纯的调用线程而已。
// m1.run();
// m2.run();

m1.start(); //start ,使线程开始执行,java虚拟机调用此线程的run方法
m2.start();
}
}

Java 多线程实现的两种方法_优先级

方法2:
实现Runnable接口

  1. 定义一个类MyRunnable实现Runnable接口
  2. 在MyRunnable类中重写run()方法
  3. 创建MyRunnable类的对象创建Thread类的对象,把MyRunnable对象作为构造方法的参数
  4. start启动线程
package dch.thread05;
//通过实现接口的方式来实现多线程,不影响他再继承其他的类
public class MyRunnable implements Runnable {
//实现runnable接口,并没有继承thread类,好处:将来可以有自己的父类,
@Override
public void run() {
for (int i=0;i<100;i++){
//这个方法会报错,因为这个类只是实现了Runnable接口,和Thread类没有关系,
//不能直接调用Thread类中的方法
// System.out.println(getName()+":"+i);
//我们先拿到当前线程,然后再去拿这个名字,就可以了
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
package dch.thread05;
/*
方式2:实现Runnable接口
1:定义一个类MyRunnable实现Runnable接口
2:在MyRunnable类中重写run()方法
3:创建MyRunnable类的对象
4:创建Thread类的对象,把MyRunnable对象作为构造方法的参数
5:启动线程
*/

public class MyRunnableDemo {
public static void main(String[] args) {
//创建myrunable类对象

MyRunnable my=new MyRunnable();
//将这个看成,同一个资源,由多个线程去使用

//创建thread类对象,把myrunnable对象作为构造方法的参数
//Thread(Runnable target)
// Thread t1=new Thread(my);
// Thread t2=new Thread(my);
// Thread(Runnable target, String name)
//将myRunnable实现类的对象作为参数传递,可以将myRunnable看作同一个资源,由多个线程去使用
Thread t1=new Thread(my,"高铁");
Thread t2=new Thread(my,"飞机");

}
}

MyRunnable my=new MyRunnable();
//将这个看成,同一个资源,由多个线程去使用

线程优先级

package xiancheng.MyThread;

public class ThreadPriority extends Thread {
@Override
public void run() {
for (int i = 0;i<100;i++){
System.out.println(getName()+":"+i);
}
}
}
package xiancheng.MyThread;

/*
Thread类中设置和获取线程优先级的方法
public final void setPriority(int newPriority):更改此线程的优先级
public final int getPriority():返回此线程的优先级
*/
public class ThreadPriorityDemo {
public static void main(String[] args) {
ThreadPriority tp1 = new ThreadPriority();
ThreadPriority tp2 = new ThreadPriority();
ThreadPriority tp3 = new ThreadPriority();

tp1.setName("高铁");
tp2.setName("飞机");
tp3.setName("汽车");

//public final int getPriority():返回此线程的优先级
System.out.println(tp1.getPriority()); //5
System.out.println(tp2.getPriority()); //5
System.out.println(tp3.getPriority()); //5

//public final void setPriority(int newPriority):更改此线程的优先级
// tp1.setPriority(10000); //IllegalArgumentException
// System.out.println(Thread.MAX_PRIORITY); //10
// System.out.println(Thread.MIN_PRIORITY); //1
// System.out.println(Thread.NORM_PRIORITY); //5

//设置正确的优先级,线程优先级高,只能表示获取cpu时间片的几率的高,并不是每一次都会跑在最前面
tp1.setPriority(5);
tp2.setPriority(10);
tp3.setPriority(1);

tp1.start();
tp2.start();
tp3.start();
}
}

代码建议能分开写,就分开写

参考链接:
​​​卖票案例​