这篇文章主要为大家展示了“Java中Thread类的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java中Thread类的示例分析”这篇文章吧。
Thread 类是 JVM 用来管理线程的一个类,换句话说,每个线程都有一个唯一的 Thread 对象与之关联。
Java中创建线程
显示继承Thread,重写run方法来指定线程执行的代码
匿名内部类来继承Thread,重写run方法来指定线程执行的代码
显示实现Runnable接口,重写run方法
匿名内部类来继承Runnable接口,重写run方法
通过lambda表达式来描述执行的代码
属性 | 获取方法 |
ID | getId() |
名称 | getNmame() |
状态 | getState() |
优先级 | getPriority() |
是否后台线程 | isDaemon() |
是否存活 | isAlive() |
是否被中断 | isInterrupted() |
ID 是线程的唯一标识,不同线程不会重复
名称是各种调试工具用到 状态表示线程当前所处的一个情况,下面我们会进一步说明
优先级高的线程理论上来说更容易被调度到
关于后台线程,需要记住一点:JVM会在一个进程的所有非后台线程结束后,才会结束运行。
是否存活,即简单的理解,为 run 方法是否运行结束了
线程的中断问题,下面我们进一步说明
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread("123"){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().getName());
try{
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程退出");
}
};
//这一组属性,线程创建完成后,属性就不变了
System.out.println(t.getName());
System.out.println(t.getPriority());
System.out.println(t.isDaemon());
System.out.println(t.getId());
//这组属性会随着线程的运行而开始改变
System.out.println(t.isAlive());
System.out.println(t.isInterrupted());
System.out.println(t.getState());
t.start();
while (t.isAlive()){
System.out.println("123 正在运行");
System.out.println(t.getState());
System.out.println(t.isInterrupted());
Thread.sleep(300);
}
}
之前我们已经看到了如何通过覆写 run 方法创建一个线程对象,但线程对象被创建出来并不意味着线程就开始运行了。
覆写 run 方法是提供给线程要做的事情的指令清单
线程对象可以认为是把 李四、王五叫过来了
而调用 start() 方法,就是喊一声:”行动起来!“,线程才真正独立去执行了。
static class MyThread extends Thread{
@Override
public void run() {
System.out.println("我是一个线程");
}
}
public static void main(String[] args) {
Thread t = new MyThread();
t.start();
}
中断让一个程序结束,结束可能有两种情况
1,已经把任务执行完了
2,任务执行到一半,被强制结束
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(){
@Override
public void run() {
while (! isQuit){
System.out.println("正在转账");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("转账终止");
}
};
t.start();
Thread.sleep(500);
System.out.println("有内鬼,终止交易");
isQuit = true;
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(){
@Override
public void run() {
while (!Thread.interrupted()){
System.out.println("正在转账");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
System.out.println("转账终止");
}
};
t.start();
Thread.sleep(5000);
System.out.println("有内鬼,终止交易");
t.interrupt();
}
thread 收到通知的方式有两种:
1. 如果线程因为调用 wait/join/sleep 等方法而阻塞挂起,则以 InterruptedException 异常的形式通 知,清除中断标志
当出现 InterruptedException 的时候, 要不要结束线程取决于 catch 中代码的写法. 可以选择 忽略这个异常, 也可以跳出循环结束线程.
2.否则,只是内部的一个中断标志被设置,thread 可以通过
Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志
Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志
这种方式通知收到的更及时,即使线程正在 sleep 也可以马上收到。
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println(Thread.interrupted());
}
}
};
t.start();
t.interrupt();
}
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().isInterrupted());
}
}
};
t.start();
t.interrupt();
}
t1与t2串行执行
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是线程1");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是线程2");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
System.out.println("主线程执行完毕");
}
t1与t2并发执行
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是线程1");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++){
System.out.println("我是线程2");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("主线程执行完毕");
}
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());
Thread.sleep(1000);
System.out.println(System.currentTimeMillis());
}
1,如果线程在正常运行计算判断逻辑,此时就是在就绪队列中排队,调度器就会从就绪队列中筛选出合适的PCB让他在CPU上运行
2,如果某个线程调用sleep就会让对应的线程的PCB进入阻塞队列,阻塞队列无法在PCB上运行
3,时间到了之后,就自动把这个PCB拿回到原来的就绪队列中
以上是“Java中Thread类的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。