大家好,通过java进阶—多线程,我们知道的什么是进程,什么是线程,以及线程的三种创建方式的选择
今天,我们来看看线程的基础操作
public class Demo implements Runnable {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("线程开启我就跑");
}
}
public static void main(String[] args) {
Demo demo =new Demo();
Thread thread =new Thread(demo);
//开启线程
thread.start();
}
}
currentThread() 获取了当前线程,里面可以获取当前线程的名字
1. getName(): 获取当前线程名字
2. getId(): 获取当前线程名字
public class Demo implements Runnable{
@Override
public void run() {
System.out.println("当前线程:"+ Thread.currentThread());
System.out.println("当前线程的名字:"+ Thread.currentThread().getName());
System.out.println("当前线程的Id:"+ Thread.currentThread().getId());
}
public static void main(String[] args) {
Demo demo =new Demo();
Thread thread = new Thread(demo);
thread.start();
}
}
为什么是 默认是 Thread-0 ,是因为 Thread类里面的一个空参构造函数,
调用 nextThreadNum() 参数是从0 开始自增的
public class Demo implements Runnable{
@Override
public void run() {
System.out.println("当前线程:"+ Thread.currentThread());
System.out.println("当前线程的名字:"+ Thread.currentThread().getName());
}
public static void main(String[] args) {
Demo demo =new Demo();
Thread thread = new Thread(demo);
thread.setName("我设置的线程名字");
thread.start();
}
}
来看一个例子,主线程(mian)同 thread-0 线程 同时去执行 循环,去抢Cpu的执行权,我们在thread-0 线程加上 如果 循环出的值 %2 ==0 (是偶数)它就释放执行权
public class Demo implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
if (i % 2 == 0) {
Thread.yield();
}
}
}
public static void main(String[] args) {
Demo demo = new Demo();
Thread thread = new Thread(demo);
thread.start();
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
当然,释放执行权并不是一定,也是有概率的,有可能下一个执行权还是 thread-0
举个例子:比如还是前面 thread-0 跟 主线程 抢占cpu执行权,现在我们在主线程中加一个 限制 条件,当主线程 循环到 10 时,让主线程进入阻塞状态,thread-0优先执行完,主线程再开始执行
public class Demo implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
if (i % 2 == 0) {
Thread.yield();
}
}
}
public static void main(String[] args) {
Demo demo = new Demo();
Thread thread = new Thread(demo);
thread.start();
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
if(i==10) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
通过 thread-0线程去调用join方法 ,让 thread-0优先执行
可以看到 mian到了 10 时,就停止了,等thread-0 执行完,再开始
stop( ) 强制结束当前线程,方法已经过时,不建议使用 (开发中不要用)
sleep(毫秒值):让当前线程进入休眠时间,时间结束继续执行
public class Demo implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
if (i %2==0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Demo demo = new Demo();
Thread thread = new Thread(demo);
thread.start();
for (int i = 0; i < 50; i++) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
当阻塞时间结束,也并不是直接执行,也得dengCPU分配给你。你再执行,在阻塞时间内,cpu 想分配资源给你都不行
public class Demo implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
if (i %2==0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Demo demo = new Demo();
Thread thread = new Thread(demo);
thread.start();
for (int i = 0; i < 50; i++) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + i);
}
System.out.println("线程是否还存活:"+thread.isAlive());
}
}
执行结果:
解析(看的懂得小伙伴可以略过): 我们在主线程中,直接加了 join, 所以得等主线程执行完成再执行主线程,由于我们是在主线程之后加的判断线程 thread-0是否存活,这时候 thread-0 已经执行完任务了,生命周期结束,所以此时的thread-0 已经终止了
以上便是线程最常用方法了