1.进程是什么?
简单来说进程就是在内存中进行的程序,每个进程也都有自己独立的内存空间,其包含一个或多个线程。
2.线程是什么?
线程就是进程中一个单一顺序的控制流,负责当前进程中程序的执行。
二、两者区别是什么 ?上面简单的说了一下进程和线程,那么他们之间的区别在哪里?进程是一种动态概念,是程序执行时相关资源分配的最小单元,其至少包含一个线程。而线程是经常的控制单元,是CPU分配资源时的最小单元,需求的资源比进程要少可以看做是一个轻量级的进程,会共享进程中的内存,线程有独立的空间(栈、程序计数器),相互通信更加方便。
三、串行,并发与并行串行:就是多个指令一次执行
并发:每个线程单独执行一段指令,一个CPU在线程间切换(并不是同时执行)
并行:多个CPU内核同时执行多个线程,线程是同时执行的
四、线程的实现方式1.继承Thread类
继承Thread会重写run()方法,然后调用start()方法来执行:
class MyThreads extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() +"执行了" +i);
}
}
}
public class MyThread{
public static void main(String[] args) {
MyThreads myThread = new MyThreads();
myThread.start();
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() +"执行了" +i);
}
}
那么run和start这两种有什么区别???
以本人浅薄的知识来解释就是,start会启动新的线程,不会对主线程有影响,而run只在主线程中执行
2.实现Runnable接口
实现run方法,然后创建实现Runnable接口的对象,传入Thread对象中,之后启动线程
class MyRunnable implements Runnable{
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}
}
public class RunnableDemo {
public static void main(String[] args) {
//创建Runnable对象,传入Thread对象
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
//启动线程
thread.start();
//匿名内部类
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}
});
thread2.start();
//Lambda表达式
new Thread(() -> {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}).start();
}
}
3.实现Callable接口
实现Callable接口可以返回 值,继承Thread类和Runnable不行
实现Callable接口,实现其call方法,创建Callable对象,传入FutureTask对象,创建FutureTask对象,将其传入到Thread对象,调用start方法启动线程,调用get方法获得返回值
package com.ren.line; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; class MyCall implements Callable五:练习 1、设计两个线程,一个线程负责打印1~100以内所有的偶数;然后,另外一个线程负责打印1~100以内所有的奇数。{ @Override public Long call() { long sum = 0; for (int i = 0; i < 1000000000L; i++) { sum += i; } return sum; } } public class CallableDemo { public static void main(String[] args) { //创建FutureTask FutureTask task = new FutureTask<>(new MyCall()); Thread thread = new Thread(task); thread.start(); System.out.println("我是等待结果,我先出来的."); try { System.out.println(task.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
public class ThreadDemo {
public static void main(String[] args) {
Thread t1 = new Thread(()->{
for (int i = 0; i <50 ; i++) {
if (i%2!=0){
System.out.println("t1--->"+i);
}
}
});
Thread t2 = new Thread(()->{
for (int i = 0; i <50 ; i++) {
if(i%2==0){
System.out.println("t2--->"+i);
}
}
});
t2.setPriority(Thread.MAX_PRIORITY);
t1.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
2、实现一个线程,用于扫描某个目录下的所有文本文件(包括:java、txt、html),并将文字内容打印出来
public class ThreadDemo4 {
static void find(String path){
File file = new File(path);
//如果是目录就列举所有文件
File[] files = file.listFiles();
if(files!=null){
for (File f:
files) {
if (!f.isDirectory()){
System.out.println(f.getAbsolutePath());
//是文件就输出
read(f.getAbsolutePath());
}else{
System.out.println("目录:" + f.getAbsolutePath());
//如果是目录,递归调用,查找子目录
find(f.getAbsolutePath());
}
}
}
}
static void read(String path){
try(FileInputStream in = new FileInputStream(path)){
//创建字节数组
byte[] buffer = new byte[300];
//每次读取长度
int length = 0;
//循环将数据读取到字节数组中,到末尾位置
while ((length = in.read(buffer))!=-1){
String str = new String(buffer,0,length);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread t1 = new Thread(()->{
find("D:\线程文件夹");
});
t1.start();
}
}
3、某人正在看电视连续剧,从第1~88集,看到第10集时,来了一个送快递的,收完快递后后,继续看电视。
public class ThreadDemo2 {
static Thread t1;
static Thread t2;
public static void main(String[] args) {
t1 = new Thread(()->{
for (int i = 0; i <= 88; i++) {
System.out.println("看电视剧第"+i+"集");
if (i==10){
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.start();
t2 = new Thread(()->{
for (int i = 0; i < 1; i++) {
System.out.println("我去拿快递"+i);
}
});
t2.start();
}
}
4、多线程模拟龟兔赛跑:乌龟和兔子进行1000米赛跑,兔子前进5米,乌龟只能前进1米。但兔子每20米要休息500毫秒,而乌龟是每100米休息500毫秒。谁先到终点就结束程序,并显示获胜方
static Thread t1;
static Thread t2;
public static void main(String[] args) {
t1 = new Thread(()->{
for (int i = 0; i <= 1000; i++) {
System.out.println("兔子跑了"+i+"米");
if (i%20==0){
try {
System.out.println("兔子开始休息了");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (i==1000){
System.out.println("兔子赢了!!!");
}
}
});
t2 = new Thread(()->{
for (int j = 0; j <= 1000; j+=5) {
System.out.println("乌龟跑了"+j+"米");
if (j%100==0){
try {
System.out.println("乌龟开始休息了");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (j==1000){
System.out.println("乌龟赢了!!!");
}
}
});
t1.start();
t2.start();
}



