JAVA多线程
一.进程
计算机正在执行的程序(.exe),负责内存的分配
研究进程的时候,本质上就是研究线程
* 一个Java应用程序(main函数)在执行的过程里面通常有几个线程在执行?
* 1.main函数是一个线程
* 2.gc线程
1.1多进程
计算机在以一个飞快的执行速度在来回的切换应用程序1.2单进程
线程
多线程
//启动线程 子线程
MyThread th1=new MyThread();
//th1.run(); //呈现出来的现象分线程没关系?why?这只是纯粹的对象调用方法,不是启动线程
th1.start(); //启动线程
//主线程执行内容
for(int j=100;j>0;j--){
System.out.println("main"+j);
}
}
}
/*自定义线程;
* 步骤:
* 1.创建一个类
* 2.继承Thread类
* 3.重写run方法
*/
class MyThread extends Thread{
@Override
public void run() {
for(int i=1;i<100;i++){
System.out.println(i);
}
}
}
一个应用程序里面有多个执行路径(执行的分配跟多进程一样,竞争CPU使用权)360软件 进程
体检 线程
杀毒 线程
单线程
线程里面的一个执行路径(分支)
自定义线程
第一种自定义线程例子
public class Demo03 {
public static void main(String[] args) {
/*研究第一种创建自动义线程的方法的特点:动车买票的情况:多个窗口卖票
*
* 1.数据非静态时不能共享
* 2.静态数据可以线程之间共享
* 3.弊端:静态变量的生命周期太长
* 4
*
*
*/
MyThread03 th1=new MyThread03();
th1.setName("窗口一");
MyThread03 th2=new MyThread03("窗口二");
MyThread03 th3=new MyThread03("窗口三");
MyThread03 th4=new MyThread03("窗口四");
th1.start();
th2.start();
th3.start();
th4.start();
}
}
class MyThread03 extends Thread{
public static int ticket=100; //票数
private String name;
public MyThread03() {
super();
}
public MyThread03(String name) {
super(name);
}
@Override
public void run() {
while(ticket>0){
System.out.println(Thread.currentThread().getName()+"D360还剩下"+ticket+"张票");
ticket--;
}
}
}
第二种自定义线程例子
public class Demo04 {
public static void main(String[] args) {
/*第二种创建自定义线程的方式 实现接口的方式
*
* 1.各线程之间实现共享
*/
Mythread04 th=new Mythread04();
Thread th1=new Thread(th);
th1.setName("窗口一");
Thread th2=new Thread(th,"窗口二");
Thread th3=new Thread(th,"窗口三");
Thread th4=new Thread(th,"窗口四");
th1.start();
th2.start();
th3.start();
th4.start();
}
}
class Mythread04 implements Runnable{
private int ticket =100;
@Override
public void run() {
try {
Thread.sleep(100); //休眠
} catch (Exception e) {
e.printStackTrace();
}
while(ticket>0){
System.out.println(Thread.currentThread().getName()+"中D10086还剩下"+ticket+"张票");
ticket--;
}
}
}
线程的加入和结束
join()方法
public class JoinDemo {
public static void main(String[] args) {
/*线程加入:join() 对象的方法
*
*/
Mythread06 th=new Mythread06();
th.setName("线程一");
th.start();
for(int j=1;j<100;j++){
if(j==50){
try {
th.join(); //线程加入 主线程进入阻塞状态,让出CPU使用权,。等待子线程执行完毕以后才有可能继续使用CPU
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"=="+j);
}
}
}
class Mythread06 extends Thread{
int i=1;
@Override
public void run() {
while(i<100){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"=="+i);
i++;
}
}
}
stop
public class StopDemo {
public static void main(String[] args) {
/*
*如何将线程结束掉?
*1.线程代买执行完毕的时候,进入死亡状态(理想的情况)
*2.使用标志位,有条件的将线程结束掉
*
*/
Mythread05 th=new Mythread05();
th.setName("线程一");
th.start();
//主线程内容
for(int i=1;i<100;i++){
if(i==50){
th.flag=false; //改变标志位,结束子线程
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"=="+i);
}
}
}
class Mythread05 extends Thread{
public boolean flag=true;
int i=1;
@Override
public void run() {
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
while(flag){
System.out.println(Thread.currentThread().getName()+"=="+i);
i++;
}
System.out.println(Thread.currentThread().getName()+"线程结束了!");
}
}