多线程学习
- 多线程
- 1. 并行与并发
- 2.进程和线程
- 3. *****多线程的实现方式
- 3.1 继承Thread类的方式进行实现
- 3.2 实现Runnable接口方式进行实现
- 3.3 利用Callable和Future接口方式实现
- 3.4 设置获取线程名字
- 4.获得线程对象
- 5.线程休眠
- 6.线程调度[线程的优先级]
- 7.后台线程/守护线程
多线程
1. 并行与并发
并行:同一时刻,有多个指令在多个CPU上同时执行
并发:同一时刻,有多个指令在单个CPU上交替执行
2.进程和线程
进程:正在运行的软件
线程:进程中的单个顺序控制流,是一条执行路径
3. *****多线程的实现方式
3.1 继承Thread类的方式进行实现
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 t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
t2.start();
}
}
- 为什么要重写run()方法?
多线程开启之后执行run()方法,run()是用来封装被线程执行的代码 - run()方法和start()方法的区别?
run方法调用不开启线程,start开启线程
3.2 实现Runnable接口方式进行实现
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程开启了"+i);
}
}
public static void main(String[] args) {
MyRunnable mr=new MyRunnable();
Thread t1=new Thread(mr);
t1.start();
MyRunnable mrs=new MyRunnable();
Thread t2=new Thread(mrs);
t2.start();
}
}
3.3 利用Callable和Future接口方式实现
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
for (int i = 0; i < 50; i++) {
System.out.println("向女孩表白"+i);
}
return "答应";
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable mt=new MyCallable();
FutureTask<String> ft=new FutureTask<String>(mt);
Thread t=new Thread(ft);
t.start();
String s = ft.get();
System.out.println(s);
}
}
get方法在线程开启之后调用
三种方式对比
3.4 设置获取线程名字
- 获取线程名字
public class Mythread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+"****"+i);
}
}
public static void main(String[] args) {
Mythread mt=new Mythread();
mt.start();
Mythread ss=new Mythread();
ss.start();
}
}
- 设置线程民资
- set形式
Mythread mt=new Mythread();
mt.start();
mt.setName("线程1");
Mythread ss=new Mythread();
ss.start();
ss.setName("线程2");
- 构造器形式
public class Mythread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+"****"+i);
}
}
public Mythread(String name) {
super(name);
}
public static void main(String[] args) {
Mythread mt=new Mythread("线程1");
mt.start();
Mythread ss=new Mythread("线程2");
ss.start();
}
}
4.获得线程对象
在runnable中可以获取线程对象名字
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"running"+i);
}
}
public static void main(String[] args) {
// MyRunnable myRunnable=new MyRunnable();
// Thread thread=new Thread(myRunnable);
// thread.setName("666");
// thread.start();
String name = Thread.currentThread().getName();
System.out.println(name);
}
}
5.线程休眠
Thread.sleep(3000);
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"running"+i);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
6.线程调度[线程的优先级]
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"***"+i);
}
return "线程执行结束";
}
public static void main(String[] args) {
MyCallable mc=new MyCallable();
FutureTask<String> ft=new FutureTask<>(mc);
Thread thread=new Thread(ft);
thread.start();
thread.setPriority(2);
thread.setName("飞机");
// System.out.println(thread.getPriority());
MyCallable mc2=new MyCallable();
FutureTask<String> ft2=new FutureTask<>(mc2);
Thread t2=new Thread(ft2);
t2.start();
t2.setPriority(10);
t2.setName("坦克");
// System.out.println(t2.getPriority());
}
}
7.后台线程/守护线程
普通线程结束 守护线程也随之结束
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName()+"****"+i);
}
}
public MyThread(String name) {
super(name);
}
public static void main(String[] args) {
MyThread mt=new MyThread("女神");
mt.start();
MyThread2 t2=new MyThread2("备胎");
t2.setDaemon(true);
t2.start();
}
}
public class MyThread2 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+"****"+i);
}
}
public MyThread2(String name) {
super(name);
}
}