进程:
进程是指运行中的程序,比如使用qq,就启动了一个进程,操作系统为该进程分配内存空间
是动态过程,有自身的产生、存在、消亡的过程
线程:
由进程创建,是进程的一个实体
一个进程可以有多个线程,比如迅雷同时下载多个文件
单线程:
同一时刻,只允许执行一个线程
多线程:
同一个时刻,可以执行多个线程,比如一个qq进程同时打开多个聊天窗口
并发:
同一个时刻,多个任务交替执行,“貌似同时”。单核cpu实现的多任务就是并发
并行:同一个时刻,多个任务同时执行,多核cpu可以实现并行
cpu1执行qq
cpu2同时执行迅雷
1.继承Thread类,重写run方法
2.实现Runnable接口,重写run方法
public class Create_ {
public static void main (String[] args) throws InterruptedException{
Cat cat = new Cat();//创建Cat对象,可以当作线程使用
cat.start();//启动线程
//当main线程启动一个线程Thread-0,主线程不会被阻塞
for (int i = 0; i < 60; i++) {
System.out.println("主线程i="+i+"线程名:"+Thread.currentThread().getName());
Thread.sleep(1000);
}
}
}
class Cat extends Thread{
int times=0;
@Override
public void run() {
while (true){
System.out.println("喵~"+(++times)+"线程名称:"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(times==80)
break;
}
}
}
执行流程:
运行程序,开启进程,创建main线程,main线程中创建Thread-0线程,main线程结束退出,几秒后,Thread-0线程结束退出,进程结束
为什么是start()方法
调用start方法:
(1).public synchronized void start(){
start0();
}
(2) start0()是本地方法,是JVM调用的,底层是c/c++实现
真正实现多线程的效果,是start0(),而不是run()
public native void start0()
底层使用了代理模式
public class Create_1 {
public static void main(String[] args) {
Dog dog = new Dog();
Thread thread = new Thread(dog);
thread.start();
}
}
class Dog implements Runnable{
int times=0;
@Override
public void run() {
while (true){
System.out.println("汪汪叫~"+(times++));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(times==10)
break;
}
}
}
dog传给Thread类中的target,调用start(),调用start0(),调用run(),动态绑定,调用Dog中的run()
模拟Thread
//线程代理类,模拟Thread类
public class Proxy{
public static void main(String[] args) {
Tiger tiger = new Tiger();
Proxy1 proxy1 = new Proxy1(tiger);
proxy1.start();
}
}
class Tiger implements Runnable{
@Override
public void run() {
System.out.println("老虎~");
}
}
//模拟Thread类
class Proxy1 implements Runnable{
private Runnable target = null;//类型是runnable
public Proxy1(Runnable target) {
this.target = target;
}
public void start(){
start0();
}
public void start0(){
run();
}
@Override
public void run() {
if(target!=null){
target.run(); //动态绑定(运行类型Tiger)
}
}
}
多线程展示
public class Thread01 {
public static void main(String[] args) {
T1 t1 = new T1();
T2 t2 = new T2();
Thread thread1 = new Thread(t1);
thread1.start();
Thread thread2 = new Thread(t2);
thread2.start();
}
}
class T1 implements Runnable{
int times=0;
@Override
public void run() {
while (true){
System.out.println("t1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(times==10)
break;
}
}
}
class T2 implements Runnable{
int times=0;
@Override
public void run() {
while (true){
System.out.println("t2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(times==10)
break;
}
}
}
继承Thread VS 实现Runnable接口
1.java的设计来看,两者没有本质区别,因为Thread也是实现了Runnable接口
start()-start0()
2.实现Runnable接口的方式,避免了单继承的限制
更加适合多个线程共享一个资源的情况
三个窗口同时售票,共100张
出现超卖问题
public class SellTicket {
public static void main(String[] args) {
SellTicket02 sellTicket021 = new SellTicket02();
new Thread(sellTicket021).start();
new Thread(sellTicket021).start();
new Thread(sellTicket021).start();
}
}
class SellTicket01 extends Thread{
private static int tcketnums=100;
@Override
public void run() {
while (true){
if(tcketnums<=0){
System.out.println("卖空");
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口"+Thread.currentThread().getName()+"卖出了票,还剩"+(--tcketnums));
}
}
}
class SellTicket02 implements Runnable{
private int tcketnums=100;
@Override
public void run() {
while (true){
if(tcketnums<=0){
System.out.println("卖空");
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口"+Thread.currentThread().getName()+"卖出了票,还剩"+(--tcketnums));
}
}
}
线程退出
通知方式退出
1.线程运行完,自动退出
2.通过变量来控制线程退出
public class Exit_ {
public static void main(String[] args) {
T1 t1 = new T1();
t1.start();
System.out.println("主线程休眠10s");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//给t1线程给传值,通知退出
t1.setflag(false);
}
}
class T1 extends Thread{
private int num =0;
//标志
Boolean flag=true;
@Override
public void run() {
while (flag){
System.out.println("线程T1正在运行:"+(num++));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//main线程给值
public void setflag(Boolean flag){
this.flag=flag;
}
}
线程常用方法
中断:interrupt
中断线程 ,不是结束线程,所以一般用于中断正在休眠的线程
t.setName(“sss”);
t.setPriority(Thread.MIN_PRIORITY);
public class MethodInterrupt {
public static void main(String[] args) throws InterruptedException {
T t = new T();
//设置名称
t.setName("sss");
//设置优先级
t.setPriority(Thread.MIN_PRIORITY);
t.start();
for (int i = 0; i <5 ; i++) {
Thread.sleep(1000);
System.out.println("hi:"+i);
}
t.interrupt();
}
}
class T extends Thread{
@Override
public void run() {
while (true){
for (int i = 0; i <100 ; i++) {
System.out.println(Thread.currentThread().getName()+"正在吃包子"+i);
}
try {
System.out.println(Thread.currentThread().getName()+"休眠中");
Thread.sleep(20000);
} catch (InterruptedException e) {
//线程执行到interrupt方法的时候,就会catch一个异常
//就是中止异常
System.out.println(Thread.currentThread().getName()+"被interrupt");
}
}
}
}
线程常用方法
1.yield:线程的礼让,让出cpu,让其他线程执行,但是礼让的时间不确定,所以也不一定礼让成功
到底让不让,由内核决定的,看cpu是否紧张
2.join:线程的插队。插队的线程一旦插入成功,肯定先执行完插入的线程
public class MethodJoinYield {
public static void main(String[] args) throws InterruptedException {
T1 t1 = new T1();
t1.start();
for (int i = 0; i < 20; i++) {
Thread.sleep(1000);
System.out.println("主线程在吃包子"+i);
if(i==5){
//主线程吃5个包子的时候,就让子线程执行完之后再执行主线程
t1.join();
//Thread.yield();
System.out.println("子线程执行完毕~");
}
}
System.out.println("主线程执行完毕");
}
}
class T1 extends Thread{
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
System.out.println("子线程在吃包子~"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("子线程执行完毕");
}
}
用户线程和守护线程
1.用户线程:工作线程,线程的任务执行完或者通知方式 结束
2.守护线程:为工作线程服务,所有用户线程结束,守护线程自动结束
t1.setDaemon(true);
垃圾回收线程就是守护线程
public class MethodDeamon {
public static void main(String[] args) throws InterruptedException {
MyDeamon myDeamon = new MyDeamon();
//把MyDeamon设置为守护线程,当main线程结束的时候,守护线程结束
myDeamon.setDaemon(true);
myDeamon.start();
for (int i = 0; i < 5; i++) {
System.out.println("老师正在来~");
Thread.sleep(1000);
}
}
}
class MyDeamon extends Thread{
@Override
public void run() {
while (true){
System.out.println("学生聊天中~");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程状态
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
T t = new T();
System.out.println(t.getName()+"状态:"+t.getState());
t.start();
while (Thread.State.TERMINATED!=t.getState()){
System.out.println(t.getName()+"状态:"+t.getState());
Thread.sleep(500);
}
System.out.println(t.getName()+"状态:"+t.getState());
}
}
class T extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Hi :"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Synchronized 线程同步机制
1.多线程编程,一些敏感数据不允许被多个线程同时访问,用同步访问技术。
保证数据在任何时刻,最多一个线程访问,保证数据的完整性
同步具体方法 Synchronized
1同步代码块
synchronized(对象){//得到对象的锁,才能操作同步代码
//需要被同步的代码
}
2.还可以放到方法声明中,表示整个方法为同步方法
public synchronized void m (String name){
//同步的代码
}
解决售卖问题
SellTicket03 sellTicket03 = new SellTicket03(); new Thread(sellTicket03).start(); new Thread(sellTicket03).start(); new Thread(sellTicket03).start();
class SellTicket03 implements Runnable{
private int tcketnums=100;
private boolean flag =true;
public synchronized void shell(){
if(tcketnums<=0){
System.out.println("卖空");
flag=false;
return;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口"+Thread.currentThread().getName()+"卖出了票,还剩"+(--tcketnums));
}
@Override
public void run() {
while (flag) {
shell();
}
}
}
互斥锁
同步方法如果没有使用static修饰,默认锁对象为this
否则,默认锁对象为 当前类.class
如果在静态方法中,实现一个同步代码块, (当前类.class)
线程死锁
多个线程都占用了对方锁资源,但是不肯想让
public class Syn_ {
public static void main(String[] args) {
T t1 = new T(true);
T t2 = new T(false);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class T extends Thread{
static Object o1 = new Object();
static Object o2 = new Object();
private boolean flag;
public T(Boolean flag){
this.flag=flag;
}
@Override
public void run() {
if(flag){
synchronized (o1) {
System.out.println(Thread.currentThread().getName() + ":" + 1);
synchronized (o2) {
System.out.println(Thread.currentThread().getName() + ":" + 2);
}
}
}
else {
synchronized (o2) {
System.out.println(Thread.currentThread().getName() + ":" + 2);
synchronized (o1) {
System.out.println(Thread.currentThread().getName() + ":" + 1);
}
}
}
}
}
释放锁
不会释放锁



