线程的基本使用
案例1
线程应用案例1-继承Thread类
1、请编写程序,开启一个线程,该线程每隔1秒,在控制台输出“喵喵,我是小猫咪"
2、对上题进行改:当输出80次时,结束该线程
3、使用JConsole监控线程执行情况,并画出程序示意图
//查看cpu核心数/线程数
/**
* @ClassName CpuNum
* @Description
* @Author 小黄debug
* @Date 2022/3/20 13:01
* @Version 1.0
**/
public class CpuNum {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
//获取当前电脑的cpu数量/线程数
int cpuNums = runtime.availableProcessors();
System.out.println("当前有cpu个数="+cpuNums);
}
}
//创建线程并使用
/**
* @ClassName Thread01
* @Description 通过继承Thread类创建线程
* @Author 小黄debug
* @Date 2022/3/20 13:06
* @Version 1.0
**/
public class Thread01 {
public static void main(String[] args) throws InterruptedException{
//创建一个Cat对象,当成线程使用
Cat cat = new Cat();
//启动线程,当start调用时,会调用run方法
//源码分析
/*
//
(1)
public synchronized void start() {
start0();
}
(2)
//start0()是一个本地方法,是JVM调用,底层是c/c++实现的
//真正实现多线程的效果,是start0(),而不是run
private native void start0();
*/
cat.start(); //启动线程 -> 最终会执行cat的run方法
//cat.run(); //如果只执行run方法,run方法就是一个普通 的方法,没有启动子线程,就会把run方法执行完毕,才向下执行
//说明:当main线程启动一个子线程,Thread-0,主线程不会阻塞,会继续执行
//这时 主线程和子线程是交替执行。。
for(int i = 0; i < 60; i++){
System.out.println("主线程在执行中i="+i+"主线程名称="+Thread.currentThread().getName());
//让主线程休眠
Thread.sleep(1000);
}
}
}
//1、当一个类继承了Thread类,该类就可以当做线程使用
//2、我们会重写run方法,写上自己的业务代码
//3、run Thread类实现了Runnable接口的run方法
/*
@Override
public void run() {
if (target != null) {
target.run();
}
}
*/
class Cat extends Thread{
int times = 0;
//重写run方法
public void run(){
while(true) {
//该线程每隔1秒,在控制台输出"喵喵,我是小猫咪"
System.out.println("喵喵,我是小猫咪"+(++times)+"子线程名称="+Thread.currentThread().getName());
//让该线程休眠1秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(times == 80){
break;
}
}
}
}
使用JConsole监控线程情况
当程序run起来后,在Termainal中输入jconsole后回车
进入到jconsole中,以不安全的方式连接,选择Thread-01(与类名相同)可以看到以下内容
案例2
说明:
1、Java是单继承的,在某些情况下一个类可能已经继承了某个父类,这时再用继承Thread类方法来创建线程显然不可能了
2、Java设计者们提供了另外一个方式创建线程,就是通过实现Runnable接口来创建线程
线程使用应用案例-实现Runnable
请编写程序,该程序 可以每隔1秒。在控制台输出"hi!",当输出10次后,自动退出,请使用Runnable接口的方式实现(这里底层使用了设计模式[代理模式-静态代理]=》代码模拟)
package com.hsydebug.threaduse;
/**
* @ClassName Thread02
* @Description 通过实现接口Runnable来开发线程
* @Author 小黄debug
* @Date 2022/3/20 14:19
* @Version 1.0
**/
public class Thread02 {
public static void main(String[] args) {
// Dog dog = new Dog();
// //dog.start();这晨不能调用start;
// //创建了Thread对象,把dog对象(实现Runnable),放入Thread
// Thread thread = new Thread(dog);
// thread.start();
//==================================
Tiger tiger = new Tiger();
ThreadProxy threadProxy = new ThreadProxy(tiger);
threadProxy.start();
}
}
class Dog implements Runnable{
int count = 0;
public void run(){ //普通方法
while(true){
System.out.println("小狗汪汪叫:hi!="+(++count)+"="+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count == 10){
break;
}
}
}
}
//========================================
class Animal{}
class Tiger extends Animal implements Runnable{
public void run(){
System.out.println("老虎叫。。。。");
}
}
//线程代理类,模拟一个极简的Thread类
class ThreadProxy implements Runnable{
private Runnable target = null; //属性:类型是Runnable
public ThreadProxy(Runnable target){
this.target = target;
}
public void run(){
if(target != null){
target.run();
}
}
public void start(){
start0();//这个方法是真正实现多线程方法
}
public void start0(){
run();
}
}