1.进程:是进行资源分配和调用的独立单元,每个进程都有自己独立的内存空间和系统资源。(正在 运行的程序)
2.线程:是进程中的一条执行线路。一个线程对应一个任务,线程是共享进程中内存空间和系统资 源。
3.进程与线程的关系:一个进程中有1个到多个线程,一个线程只属于一个进程。
一个进程中多个线程是互抢资源,竟争的关系。
将互抢资源(独立的内存空间和系统资源)叫cpu时间片。
4.实现线程的方式(前两种是重点,第三种知道就行)
4.1:声明一个类继承Thread,重写任务方法run(),实现线程类
eg:/**
* 线程类
* @author sx
* @version 1.0 2019年7月15日
*/
public class MyThread extends Thread{
/**
* 任务方法
*/
@Override
public void run() {
for (int i = 1; i <=10; i++) {
//System.out.println(this.getName()+":"+i);
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
public static void main(String[] args) {
//创建线程对象
MyThread t1=new MyThread();
MyThread t2=new MyThread();
//调用方法给线程取名
t1.setName("线程1");
t2.setName("线程2");
//启动线程
t1.start();
t2.start();
}
4.2:实现runnable接口,重写run()的方式实现线程
eg:/**
* 任务类
* @author sx
* @version 1.0 2019年7月15日
*/
public class MyRunnable implements Runnable{
/**
* 任务方法
*/
@Override
public void run() {
for (int i = 1; i <=10; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
public static void main(String[] args) {
//创建任务对象
MyRunnable m1=new MyRunnable();
MyRunnable m2=new MyRunnable();
//将任务对象封装成线程对象
Thread t1=new Thread(m1, "A线程");
Thread t2=new Thread(m2, "B线程");
//启动线程
t1.start();
t2.start();
}
4.3:(现在公司基本不用)实现Callable接口,重写call()的方式实现线程
eg: /**
* 任务类的前身
* @author sx
* @version 1.0 2019年7月15日
*/
public class MyCallable implements Callable{
/**
* 任务方法的前身
*/
@Override
public Object call() throws Exception {
for (int i = 1; i <=10; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
}
return null;
}
}
public static void main(String[] args) {
//创建callable对象
MyCallable c1=new MyCallable();
MyCallable c2=new MyCallable();
//将callable对象封装成任务对象
FutureTask task1=new FutureTask(c1);
FutureTask task2=new FutureTask(c2);
//将任务对象封装成线程对象
Thread t1=new Thread(task1, "1线程");
Thread t2=new Thread(task2, "2线程");
//启动线程
t1.start();
t2.start();
}
5.给线程取名:
5.1:用默认的线程名称。
5.2:通过构造方法给线程取名。
5.3:调用线程的setName()给线程取名。
6.继承Thread VS 实现Runnable接口
6.1:代码简洁性:继承Thread类方式实现线程代码更简洁,实现Runnable接口方式实现线程代 码复杂点。
6.2:扩展性: 继承Thread类方式实现线程类不能再继承其他的类,只可以实现其他接口,扩 展性稍差点;实现Runnable接口方式实现线程还可以继承其他的类,实现其 他的接口,扩展性好。
6.3:共享资源:继承Thread类方式实现线程,只能静态属性实现资源共享,比较耗内存。
实现Runnable接口方式实现线程,可以通过多个线程共享一个任务对象来实现 资源共享,共享性更好。
7.线程的优先级:优先级越高的线程,抢占cpu时间片概率高,优先级越低的线程抢占cpu时间片概率 低。
使用:线程对象.setPriority(int newPriority) ;必须在线程启动前设置才有效。
eg:public static void main(String[] args) {
//创建线程对象
MyThread t1=new MyThread();
MyThread t2=new MyThread();
//调用方法给线程取名
t1.setName("线程1");
t2.setName("线程2");
//设置线程的优先级
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
//启动线程
t1.start();
t2.start();
}
8.线程休眠:让当前线程在休眠期内让出cpu时间片,不参加抢占,一旦休眠时间到了,当前的线程 重新参与cpu时间片抢占。
使用:Thread.sleep(long millis); 或者线程对象.sleep(long millis);
eg:public static void main(String[] args) throws InterruptedException {
//声明一个数据存6位学员的姓名
String[] sname= {"张三","李四","王五","赵六","钱七","王八"};
//生成0-5随机数,作为随机抽中学员的下标
int r=(int)(Math.random()*6);
//三秒输出
for (int i = 3; i >=1; i--) {
//当前的线程睡眠1s
Thread.sleep(1000);
System.out.println(i);
}
System.out.println("抽中学员的姓名:"+sname[r]);
}
9.线程礼让:暂停当前正在执行的线程对象,并执行其他线程。
使用:Thread.yield(); 或者 线程对象.yield();
eg:/**
* 线程类
* @author sx
* @version 1.0 2019年7月15日
*/
public class MyThread extends Thread{
/**
* 任务方法
*/
@Override
public void run() {
for (int i = 1; i <=100; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
//如果当前线程是线程B,每运行一次就礼让一次
if (Thread.currentThread().getName().equals("线程B")) {
Thread.yield();
}
}
}
}
public static void main(String[] args) {
//创建线程对象
MyThread t1=new MyThread();
MyThread t2=new MyThread();
//调用方法给线程取名
t1.setName("线程A");
t2.setName("线程B");
//启动线程
t1.start();
t2.start();
}
10.线程合并:让多个线程合并成一个线程,让合并过来的线程先执行完,再执行原来的线程。
使用:线程对象.join();
10.1:子线程合并到主线程:子线程先运行完再运行主线程
eg:/**
* 线程类
* @author sx
* @version 1.0 2019年7月15日
*/
public class MyThread1 extends Thread{
/**
* 任务方法
*/
@Override
public void run() {
for (int i = 1; i <=200; i++) {
System.out.println(this.getName()+":"+i);
}
}
}
public static void main(String[] args) throws InterruptedException {
//创建一个子线程对象
MyThread1 t1=new MyThread1();
//设置线程的名称
t1.setName("子线程");
//启动子线程
t1.start();
for (int i = 1; i <=200; i++) {
System.out.println("主线程:"+i);
//当主线程运行到10,让子线程合并过程
if (i==10) {
t1.join();
}
}
}
10.2:子线程A合并子线程B:子线程A先运行完再运行子线程B
eg:/**
* 线程类
* @author sx
* @version 1.0 2019年7月15日
*/
public class MyThread2 extends Thread{
//声明线程属性
public Thread t;
/**
* 任务方法
*/
@Override
public void run() {
for (int i = 1; i <=200; i++) {
System.out.println(this.getName()+":"+i);
//线程B运行10时候,让线程A合并过来
if (i==10&&this.getName().equals("子线程B")) {
//让线程A合并过来
try {
this.t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws InterruptedException {
//创建一个子线程对象
MyThread2 t1=new MyThread2();
MyThread2 t2=new MyThread2();
//设置线程的名称
t1.setName("子线程A");
t2.setName("子线程B");
//将线程A当作线程B的属性传过去
t2.t=t1;
//启动子线程
t1.start();
t2.start();
}
11.线程中断
11.1:调用interrupt(),isInterrupted()来实现线程中断
eg:/**
* 线程类
* @author sx
* @version 1.0 2019年7月16日
*/
public class MyThread1 extends Thread{
/**
* 任务方法
*/
@Override
public void run() {
for (int i = 1; i <=200; i++) {
//当子线程的中断状态等于true时,跳出循环
if (this.isInterrupted()==true) {
break;
}
System.out.println(this.getName()+":"+i);
}
}
}
public static void main(String[] args) throws InterruptedException {
//创建线程对象
MyThread1 t1=new MyThread1();
//给线程取名
t1.setName("子线程");
//启动线程
t1.start();
for (int i = 1; i <=200; i++) {
System.out.println("主线程:"+i);
//当主线程运行到10时,改变子线程的中断状态
if (i==10) {
t1.interrupt();
Thread.sleep(2000);
}
}
}
11.2:用变量作标记来中断线程
eg:/**
* 线程类
* @author sx
* @version 1.0 2019年7月16日
*/
public class MyThread2 extends Thread{
//声明一个变量作标记,标记是否中断子线程
public boolean flag=false;
/**
* 任务方法
*/
@Override
public void run() {
for (int i = 1; i <=200; i++) {
//判断中断标记
if (flag) {
break;
}
System.out.println(this.getName()+":"+i);
}
}
}
public static void main(String[] args) throws InterruptedException {
//创建线程对象
MyThread2 t1=new MyThread2();
//给线程取名
t1.setName("子线程");
//启动线程
t1.start();
for (int i = 1; i <=200; i++) {
System.out.println("主线程:"+i);
//当主线程运行到10时,改变子线程的中断状态
if (i==10) {
t1.flag=true;//改变子线程的中断标记
Thread.sleep(2000);
}
}
}
12.线程生命周期
12.1:新建状态:刚New出来的线程就处于新建状态。
12.2:就绪状态:当线程调用start()时或者线程sleep()时间到了或者wait()的线程被唤 醒,线程就处于就绪状态。
12.3:运行状态:当就绪状态的线程抢到cpu时间片,执行run()时,线程就处于运行状态。
12.4:阻塞状态:当线程调用sleep()或者是wait()时,线程就处于阻塞。
12.5:死亡状态:当线程不再执行了(线程执行完了或者线程中断了…)就处于死亡状态。
13.(扩展)守护线程(精灵线程):守护非守护线程的,一旦这个进程中所有的非守护线程死亡了, 这个守护就自动死亡。
使用:线程对象.setDaemon(true);必须在线程启动前设置才有效。
eg:/**
* 线程类
* @author sx
* @version 1.0 2019年7月16日
*/
public class MyThread extends Thread{
@Override
public void run() {
for (int i = 1; i < 1000; i++) {
System.out.println(this.getName()+":"+i);
}
}
}
public static void main(String[] args) {
//创建线程对象
MyThread t1=new MyThread();
//给线程取名
t1.setName("守护线程");
//设置当前的线程为守护线程
t1.setDaemon(true);
//启动线程
t1.start();
for (int i = 1; i <= 10; i++) {
System.out.println("主线程:"+i);
}
}