💁 个人主页:黄小黄的博客主页
❤️ 支持我:👍 点赞 🌷 收藏 🤘关注
🎏 格言:All miracles start from sometime somewhere, make it right now.
本文来自专栏:JavaSE从入门到精通

🐱 下面的例子中获取了当前电脑cpu的核心数量:
/**
* @author 兴趣使然黄小黄
* @version 1.0
* 获取当前电脑的cpu核心数
*/
public class ThreadTest01 {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
// 获取当前电脑cpu数量/核心
int cpuNums = runtime.availableProcessors();
System.out.println("当前电脑的cpu核心数:" + cpuNums);
}
}
第一种使用方式为,继承Thread类,并重写run方法

🦁 案例演示:
(1)编写一个程序,开启一个线程,该线程每相隔1秒,在控制台输出:“我是大黄”;
(2)进行改进,当输出10次时,结束该线程;
(3)使用JConsole监控线程执行情况,并画出程序示意图。
/**
* @author 兴趣使然黄小黄
* @version 1.0
* 通过继承Thread使用线程
*/
public class ThreadTest02 {
public static void main(String[] args) {
// 创建一个DaHuang对象,可以当作线程使用
DaHuang daHuang = new DaHuang();
daHuang.start(); // 启动线程
}
}
class DaHuang extends Thread{
/**
次数
*/
int times = 0;
@Override
public void run() {// 重写run方法,自己的业务逻辑
while (true) {
System.out.println("我是大黄" + (times + 1) + "次");
times++;
// 休眠1秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (times == 10){
break;
}
}
}
}

在程序中,我们可以使用 Thread.currentThread().getName()来获取当前线程的名称。上述程序 执行过程中,会打开一个进程,进入main主线程。当执行start时,会执行Thread-0子线程。 示意图如下:

需要特别注意的是,Thread-0子线程的执行,并不会造成main主线程的阻塞! 修改主方法如下,结果可以看到,两个线程都在执行:
public static void main(String[] args) {
// 创建一个DaHuang对象,可以当作线程使用
DaHuang daHuang = new DaHuang();
daHuang.start(); // 启动线程
for (int i = 0; i < 10; i++) {
System.out.println("i = " + i + ", 线程" + Thread.currentThread().getName());
}
}

上述代码中,使用start后,会调用run。但是,如果通过直接使用run方法,则是由主线程调用的!代码如下:
public static void main(String[] args) {
// 创建一个DaHuang对象,可以当作线程使用
DaHuang daHuang = new DaHuang();
daHuang.run();
}

此时,由于run是主线程调用的,那么,就不是真正意义的多线程。run仅仅是一个普通的方法,没有真正的启动一个线程。 在主线程中,必须要当前run这条语句的动作执行完毕,才能继续向后执行,造成了阻塞。



由于java是单继承的,在某些情况下,一个类可能已经继承了某个父类,这时再用继承Thread类的方法来创建线程显然是不可能的了。
因此,java设计者,提供了另一种方法,就是通过实现Runnable接口来创建线程。
🦁 案例演示:
请编写程序,该程序可以每隔1秒,在控制台输出 “你好!”,当输出5次后,自动退出。请通过实现Runnable接口的方式来实现。
/**
* @author 兴趣使然黄小黄
* @version 1.0
* 通过实现Runnable接口的方式创建线程
*/
public class ThreadTest03 {
public static void main(String[] args) {
Say say = new Say();
// 创建Thread对象,把say对象(实现了Runnable),放入Thread
Thread thread = new Thread(say);
thread.start();
}
}
class Say 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 == 5){
break;
}
}
}
}

使用实现Runnable的方式创建线程,底层使用了设计模式——代理模式。
🐘 简单模拟:
/**
* @author 兴趣使然黄小黄
* @version 1.0
*/
public class ThreadTest03 {
public static void main(String[] args) {
Dog dog = new Dog();
//dog实现了Runnable接口
ThreadProxy threadProxy = new ThreadProxy(dog);
threadProxy.start();
}
}
/**
* 线程代理类
*/
class ThreadProxy implements Runnable{
/**
* 属性,类型为 Runnable
*/
private Runnable target = null;
@Override
public void run() {
if (target != null){
target.run();//动态绑定(运行类型)
}
}
public ThreadProxy(Runnable target){
this.target = target;
}
public void start(){
start0();
}
public void start0(){
run();
}
}
class Animal{}
class Dog extends Animal implements Runnable{
@Override
public void run() {
System.out.println("Dog 汪汪汪");
}
}

🐱 说明:
🌟以上便是本文的全部内容啦,后续内容将会持续 免费更新,如果文章对你有所帮助,麻烦动动小手点个赞 + 关注,非常感谢 ❤️ ❤️ ❤️ !
如果有问题,欢迎私信或者评论区!

共勉:“你间歇性的努力和蒙混过日子,都是对之前努力的清零。”
