进: 正在进行; 程: 程序
程序:程序是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念;
进程:进程是执行程序的一次执行过程,它是一个动态的概念。是系统资源分配的单位
程序是静止的,只有真正运行时的程序,才被称为进程。
目前操作系统都是支持多进程,可以同时执行多个进程,通过进程ID区分。
概念: 线程,又称轻量级进程(Light Weight Process)。
注意:很多多线程都是模拟出来的,真正的多线程是指有多个CUP,即多核,如服务器。如果是模拟出来的多线程,即在一个CPU的情况下,在同一时间点,CPU只能执行一个代码,只是因为切换的太快,所以便产生了同时执行的错觉;
任何一个线程都具有基本的组成部分:
注意:线程调用不一定立即执行,由CPU调度执行
运行程序时, JVM会自动创建主线程(main), main线程执行main方法
MyThread01:
public class MyThread01 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("子线程...."+i);
}
}
}
Test:
public class Test {
public static void main(String[] args) {
//创建子线程
MyThread01 thread01 = new MyThread01();
//启动子线程
thread01.start();
//thread01.run();调用方法
//main线程执行for
for (int i = 0; i < 100; i++) {
System.out.println("main线程......"+i);
}
}
}
面试题: 一个线程能启动两次吗?
答: 不能, 启动多次会出现IllegalThreadStateException错误
获取线程ID和名称
方式 1 :
//方式1 this.getName() this.getId()
System.out.println(this.getName()+" "+this.getId()+"子线程...."+i);
方式 2 :
//方式2 Thread.currentThread().getName()/getId()
System.out.println(
Thread.currentThread().getName()+"..."
+Thread.currentThread().getId()+"子线程...."+i
);
修改线程名称
方式 1 :
//方式1 thread.setName()
thread01.setName("线程-01");
thread02.setName("线程-02");
方式 2 :
//方式2 构造方法
//重写构造方法
public MyThread01(String name) {
super(name);
}
//创建子线程时,修改
MyThread01 thread01 = new MyThread01("线程-01");
MyThread01 thread02 = new MyThread01("线程-02");
课堂案例
实现4个窗口各卖100张票?
内存分析:
SaleTicket:
package StageOne.day19.MyThread;
/**
* @author 胡昊龙
* @version 1.0
* @description: TODO
* @date 2024/1/12 15:22
*/
public class SaleTicket extends Thread{
//总票数
private int count = 100;
//重写构造方法
public SaleTicket(String name) {
super(name);
}
@Override
public void run() {
while (count > 0) {
System.out.println(Thread.currentThread().getName() +
"卖了第" + count + "张票");
count--;
}
}
}
Test:
public class Test2 {
public static void main(String[] args) {
SaleTicket s1 = new SaleTicket("窗口1");
SaleTicket s2 = new SaleTicket("窗口2");
SaleTicket s3 = new SaleTicket("窗口3");
SaleTicket s4 = new SaleTicket("窗口4");
s1.start();
s2.start();
s3.start();
s4.start();
}
}
实现Runnable接口
MyRunnable:
package StageOne.day19.MyRunnable;
/**
* @author 胡昊龙
* @version 1.0
* @description: TODO
* @date 2024/1/12 16:02
*/
public class MyRunnable implements Runnable{
//重写run()方法
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(
Thread.currentThread().getName()+"子线程"+"..."+i
);
}
}
}
Test:
public class Test {
public static void main(String[] args) {
//创建可运行对象
MyRunnable runnable = new MyRunnable();
//创建线程对象
Thread t1 = new Thread(runnable,"线程-01");
Thread t2 = new Thread(runnable,"线程-02");
Thread t3 = new Thread(runnable,"线程-03");
//启动线程
t1.start();
t2.start();
t3.start();
//main
for (int i = 0; i < 100; i++) {
System.out.println(
"主线程..............."+i
);
}
}
}
课堂案例 1
实现4个窗口共卖100张票?
内存分析:
Ticket:
public class Ticket implements Runnable{
private int count = 100;
@Override
public void run() {
while (count > 0) {
System.out.println(Thread.currentThread().getName() +
"卖了第" + count + "张票");
count--;
}
}
}
Test:
public class Test01 {
public static void main(String[] args) {
//创建票对象(可运行对象)
Ticket ticket = new Ticket();
//创建线程对象
Thread t1 = new Thread(ticket,"窗口1");
Thread t2 = new Thread(ticket,"窗口2");
Thread t3 = new Thread(ticket,"窗口3");
Thread t4 = new Thread(ticket,"窗口4");
//启动线程
t1.start();
t2.start();
t3.start();
t4.start();
}
}
课堂案例 2
你和你女朋友共用一张银行卡,你向卡中存钱,你女朋友从卡中取钱,使用程序模拟过程?
BankCard:
public class BankCard {
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
Save:
public class Save implements Runnable{
private BankCard card;
public Save(BankCard card) {
this.card = card;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
card.setMoney(card.getMoney()+1000);
System.out.println(Thread.currentThread().getName()+"第"+i+"次存钱存了1000元," +
" 当前余额为" + card.getMoney());
}
}
}
Withdraw:
public class Withdraw implements Runnable{
private BankCard card;
public Withdraw(BankCard card) {
this.card = card;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if (card.getMoney()>=1000) {
card.setMoney(card.getMoney()-1000);
System.out.println(Thread.currentThread().getName()+"取走了1000元"
+"当前余额为"+card.getMoney());
} else {
System.out.println("当前余额不足...");
i--;//取款失败不计数,保证把钱取完
}
}
}
}
Test:
public class Test {
public static void main(String[] args) {
//卡
BankCard card = new BankCard();
//存钱
Save save = new Save(card);
//取钱
Withdraw withdraw = new Withdraw(card);
//线程
Thread t1 = new Thread(save,"小明");
Thread t2 = new Thread(withdraw,"小红");
t1.start();
t2.start();
}
}