了解线程、创建线程写法

 

认识线程

1. 什么是多线程编程

现在cpu, 都是多核心cpu

多线程编程就是通过特定的技巧, 把一个任务拆分成几个部分, 然后到不同的cpu核心上去运行

 

 

2. 什么是线程

线程是进程的一部分, 一个pcb结构体描述一个线程, 多个pcb结构体对象(多个线程) 描述一个进程

 

同一个进程的, 线程之间共享内存资源和文件资源但是每个线程都是独立的在cpu上调度执行

 

3. 为什么需要线程

每次创建/销毁 进程都会去申请资源, 开销大, 所以引入了线程

 

只有创建第一个线程 (进程), 会去申请资源, 之后创建其他线程就都不会去申请资源了

因为线程之间, 共享内存资源和文件资源

 

4. 进程和线程之间的区别与联系

1. 进程包含线程! 一个进程里面可以有一个线程,也可以有多个线程。

2. 进程在频繁创建和销毁中,开销更高. 而只有第一次创建线程(进程), 才会申请资源, 之后创建线程都不会在申请资源, 因为线程之间资源共享

3. 进程是系统分配资源(内存,文件资源....) 的基本单位。线程是系统调度执行的基本单位(CPU)

4. 进程之间是相互独立的, 一个进程挂了其他进程一般都没事. 但是, 在一个进程内部的多个线程之间, 一个线程挂了,整个进程都挂了

 

创建线程的写法

1. 创建thread子类, 重写run()方法

查看代码
 class MyThread extends Thread {
@Override
public void run() {
// 这里写的代码, 就是该线程要完成的工作
while (true) {
System.out.println("hello thread");
// 让线程主动进入"堵塞状态", 短时间停止去cpu上执行
// 单位 1000 ms (毫秒) => 1 s (秒)
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Test {
public static void main(String[] args) {
// Thread 类表示线程
Thread t = new MyThread();
t.start(); // 创建线程, 在线程中调用run()
while (true) {
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

 

 

2. 通过Runable接口

查看代码
class MyRunnable implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Test {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start();
while (true) {
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

 两种写法的区别

 

3. 匿名内部类方式

本质上和方法一是一样的

class test {
public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
while (true) {
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

 

4. 匿名内部类 针对Runnable

class test {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
while (true) {
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

 

5. lambda 表达式(推荐)

class test {
public static void main(String[] args) {
// 5. lambda 表达式 ()
Thread t = new Thread( () -> {
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
while (true) {
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

 

 

6.  callable 实现

package Thread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class demo1 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int ret = 0;
for (int i = 1;i <= 1000;i++) {
ret += i;
}
return ret;
}
};
FutureTask<Integer> futureTask = new FutureTask<>(callable);
Thread t = new Thread(futureTask);
t.start();
t.join();
System.out.println(futureTask.get());
}
}

 

posted @   qyx1  阅读(14)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示