java_线程

English

jconsole		java控制台
thread			线程、线、条状物体
priority		优先级、优先事项
interrupt		中断线程、打断、打扰
yield			线程礼让、产生
join			线程插队、参加,连接、结合
daemon			守护进程




线程介绍

一、介绍

进程: 是程序的一次执行过程,或是正在运行的一个程序,是一个动态过程,有自身的 产生、存在、消亡的过程

单线程: 同一个时刻只允许一个线程
多线程:同一个时刻,可以执行多个线程

并发: 同一个时刻,多个任务交替执行,单核cpu实现的多任务就是并发
并行: 同一个时刻,多个任务同时执行,多核cpu可以实现并行

二、获取本地Cpu个数

package 线程;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class get_cpu {
    public static void main(String[] args) {
//        获取本地电脑cpu个数
        Runtime runtime=Runtime.getRuntime();
        int CpuNums=runtime.availableProcessors();
        System.out.println(CpuNums);
    }
}

线程的使用

一、开启一个线程的两种方法
1、继承 Thread 来开启

package 线程;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class Start_Thread {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.start();   //表示开启了一个线程
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程名" + Thread.currentThread().getName()+":次数" +i);
        }
    }
}
//继承Thread开启一个线程
class Cat extends  Thread{
    private int count=0;
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                System.out.println("线程名:"+Thread.currentThread().getName() +":次数"+ ++count);
                if (count>=10){
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2、实现 Runnable 开启

package 线程;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class Start_Thread {
    public static void main(String[] args) {
        Cat cat = new Cat();
//        new个 Thread
        Thread thread=new Thread(cat);
        thread.start();     //表示开启了一个线程
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程名" + Thread.currentThread().getName()+":次数" +i);
        }
    }
}
//继承Thread开启一个线程
class Cat implements Runnable{
    private int count=0;
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                System.out.println("线程名:"+Thread.currentThread().getName() +":次数"+ ++count);
                if (count>=10){
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

线程方法

一、第一组常用的方法

package 线程;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class Thread_Method {
    public static void main(String[] args) throws InterruptedException {
        T t = new T();
//        一、设置线程的名称
        t.setName("浩浩");
//        二、设置线程的优先级  有 MIN(1) MAX(10) NORM(5) 三个等级
        t.setPriority(Thread.MIN_PRIORITY);
//        三、开启一个线程
        t.start();
//        四、获取线程的优先级
        System.out.println("线程的优先级:"+t.getPriority());
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+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(200000);
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName() + "被 interrupt 中断了...");
                }
        }
    }
}

二、第二组常用的方法

package 线程;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class Thread_Method_2 {
    public static void main(String[] args) throws InterruptedException {
/**
        yield:线程礼让,让出cpu,让其他的线程先执行 ,但礼让的时间不确定 所有不一定礼让成功
        join :线程插队,插队一旦成功,则肯定先执行完插入的线程的所有任务
 */
        zh zh = new zh();
        zh.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("hi"+i);
            Thread.sleep(1000);
            if (i==4){
//                yield 线程礼让, 不一定礼让成功
//                Thread.yield();
                
//                join 线程插队
                zh.join();
            }
        }
    }
}
class zh extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("hello"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

线程的生命周期

一、用户线程:也叫工作线程,当线程的任务执行完或通知完方式结束
二、守护线程: 一般是为工作线程服务,当所有的用户线程结束,守护线程自动结束

package 线程;

import java.util.TreeMap;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class Thread_daemon {
    public static void main(String[] args) {
        /**
         一、用户线程:也叫工作线程,当线程的任务执行完或通知完方式结束
         二、守护线程: 一般是为工作线程服务,当所有的用户线程结束,守护线程自动结束
         */
        T1 t1 = new T1();
//        设置t1为守护线程  当所有的线程执行完毕时候 t1 也会跟着结束
        t1.setDaemon(true);
        t1.start();

        T2 t2=new T2();
        t2.start();
        for (int i = 0; i < 5; i++) {
            System.out.println("main在执行...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
}
class T1 extends Thread{
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程在执行...");
        }
    }
}

class  T2 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("线程1正在执行...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

synchronized

一、线程同步机制
1、在多线程编程,一些敏感数据不允许被多个线程同时访问,此时就使用同步访问技术,保证数据在任何同一时刻,最多有一个线程访问,保证数据的完整性

package 线程;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class Thread_synchronized {
    public static void main(String[] args) {
        T3 t31 = new T3();
       new Thread(t31).start();
       new Thread(t31).start();
       new Thread(t31).start();
    }
}
class T3 implements Runnable{
    private  int count=100;
    boolean loop =true;

//    一、方法锁 加在方法上
    public /*synchronized*/ void m() {
//        如果没有使用       默认的锁是 对象(this)
//        如果使用了 static 默认的锁是 对象.class

//        二、对象锁 加在对象上面
        synchronized (this) {
            if (count <= 0) {
                System.out.println("售票结束...");
                loop = false;
                return;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票" + "剩余票数=" + (--count));

        }
    }
    @Override
    public void run() {
        while (loop){
            m();
        }
    }

}

互斥锁

package 线程;

/**
 * @author ZhouHao
 * @version 1.0
 * 成为想成为的人
 */
public class Thread_synchronized {
    public static void main(String[] args) {
        T3 t31 = new T3();
        T3 t32 = new T3();
        T3 t33 = new T3();
        t31.start();
        t32.start();
        t33.start();
    }
}
class T3 extends Thread{
    private static int count=100;
    boolean loop =true;

    public  void m() {
//        如果没有使用       默认的锁是 对象
//        如果使用了 static 默认的锁是 对象.class
        synchronized (T3.class) {
            if (count <= 0) {
                System.out.println("售票结束...");
                loop = false;
                return;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票" + "剩余票数=" + (--count));

        }
    }
    @Override
    public void run() {
        while (loop){
            m();
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有心肝,只有干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值