知了堂|Java线程详解(附实例)

72 篇文章 1 订阅

1、线程的状态
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


```java
package com.cjg.xiancheng;
public class threadStop implements Runnable   {
    //如果不填 boolean的默认值为flase
    private  boolean flag=true;
    @Override
    public void run() {
        int i =1;
       while (flag){
           System.out.println("****"+i++);
       }
    }
    public void stop(){
        this.flag=false;
    }
    public static void main(String[] args) {
        threadStop threadStop = new threadStop();
        new Thread(threadStop,"me").start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+i);
            if (i==500){
                threadStop.stop();
                System.out.println("stop=============================================================================");
            }
        }
    }
}


```java
package com.cjg.xiancheng;
import java.text.SimpleDateFormat;
import java.util.Date;
public class threadTime {
    public static void main(String[] args) throws InterruptedException {
        f();
        while (true){
            //打印当前系统的时间
            Date date = new Date(System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println(new SimpleDateFormat("yyyy--MM--dd HH:mm:ss").format(date));
        }
    }
    public static void f() throws InterruptedException {
        int i=10;
        while (true){
            Thread.sleep(100);
            System.out.println(i--);
            if (i==0)break;
        }
    }
}

2、线程的礼让

在这里插入图片描述
在这里插入图片描述

package com.cjg.xiancheng;
public class threadYield {
    public static void main(String[] args) {
        didi didi = new didi();
        new Thread(didi,"一号弟弟").start(); new Thread(didi,"二号弟弟").start();
    }
}
class didi implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"start");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"end");
    }
}

Join

◆Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞。

package com.cjg.xiancheng;
public class threadJoin implements  Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(Thread.currentThread().getName()+i);
        }
    }
    public static void main(String[] args) throws InterruptedException {
        threadJoin threadJoin = new threadJoin();
        Thread thread = new Thread(threadJoin, "vip");
                thread.start(); //线程已经开始
        for (int i = 0; i < 500; i++) {
            System.out.println(Thread.currentThread().getName()+i);
            if (i==100){
                //知道join线程结束都只执行join线程
                thread.join();
            }
        }
    }
}

3、线程的状态的查看

在这里插入图片描述

package com.cjg.xiancheng;
public class threadStatus {
    public static void main(String[] args) throws InterruptedException {
        Thread thread =new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("============================================================================");
        });
        Thread.State state = thread.getState();
        System.out.println(state);
        thread.start();
        while (state !=Thread.State.TERMINATED){
            Thread.sleep(100);
          state = thread.getState();
            System.out.println(state);
        }
    }
}

4、线程的优先级
优先级低只是意味着获得调度的概率低。并不是优先级低就不会被调用了。这都是看CPU的调度。

可能会造成性能倒置

package com.cjg.xiancheng;
public class threadMore {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"的"+Thread.currentThread().getPriority());
        more more = new more();
        Thread thread = new Thread(more);  Thread thread1 = new Thread(more);  Thread thread2 = new Thread(more);
        thread.setPriority(Thread.MAX_PRIORITY);
        thread1.setPriority(4);
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread.start(); thread1.start(); thread2.start();
    }
}
class more implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"的"+Thread.currentThread().getPriority());
    }
}

5、线程守护
◆线程分为用户线程和守护线程

◆虚拟机必须确保用户线程执行完毕

◆虚拟机不用等待守护线程执行完毕

◆如:后台记录操作日志、监控内存、垃圾回收等待……

package com.cjg.xiancheng;
public class threadDaemon {
    public static void main(String[] args) {
        me me = new me();
        parents parents = new parents();
        //设置守护线程
        Thread thread = new Thread(me);
        thread.setDaemon(true);
        thread.start();
        new Thread(parents).start();
    }
}
class me implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("我守护着你");
        }
    }
}
class parents implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <= 36500; i++) {
            System.out.println("爸妈开心的活了"+i);
        }
        System.out.println("happy end");
    }
}

6、线程同步并发
队列加锁,保证线程安全,对应sql数据库的回滚。

◆由于同一进程的多个线程共享同一块存储空间,在带来方便的同时,也带来了访问冲突问题,为了保证数据在方法中被访问时的正确性,在访问时加入锁机制synchronized,当一个线程获得对象的排它锁,独占资源,其他线程必须等待,使用后释放锁即可,存在以下问题:

◆一个线程持有锁会导致其他所有需要此锁的线程挂起;

◆在多线程竞争下,加锁、释放锁会导致比较多的上下文切换和调度延时,引 起性能问题;

◆如果一个优先级高的线程等待一个优先级低的线程释放锁,会导致优先级倒置,引起性能问题。

每个线程在自己的工作内存交互,内存控制不当会造成数据不一致

package com.cjg.xiancheng;
public class syn {
    public static void main(String[] args) {
        buy buy = new buy();
        new Thread(buy,"11111").start();  new Thread(buy,"2222222222").start();  new Thread(buy,"33333333333").start();
    }
}
class  buy implements  Runnable{
    private boolean flag=true;
    private int tiket = 10;
    @Override
    public void run() {
        while (flag){
            try {
                buyTikets();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private synchronized void buyTikets() throws InterruptedException {
        if (tiket<=0){
            flag=false;
            return;
        }
        System.out.println(Thread.currentThread().getName()+"获得第"+tiket--+"票");
        Thread.sleep(100);
    }
}
//之前加锁的是 this  如果加锁不为 this 用程序块代替  . 注意索的是谁 ,需要增删改的对象
synchronized (对象 obj){
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
了课是一个在线教育平台,提供了丰富多样的课程内容。其中,flask web是其中一门非常受欢迎的课程。 Flask是一个基于Python语言开发的轻量级Web应用框架。它简洁易用,但功能强大,因此在Web开发中被广泛应用。Flask Web课程旨在教授学员如何使用Flask框架构建自己的Web应用程序。 在Flask Web课程中,学员将学习到如何搭建一个完整的Web应用程序。首先,课程会介绍Flask框架的基本概念和使用方法,学员将了解如何创建Flask应用和处理路由。接着,课程会涵盖数据库的使用,学员将学会如何与数据库进行交互,以存储和检索数据。 此外,Flask Web课程还会教授学员如何处理表单数据和用户认证。表单是Web应用中常见的用户输入形式,学员将学习如何处理表单数据,并对用户输入进行验证和处理。同时,课程还会介绍用户认证和授权的方法,以确保只有授权用户可以访问特定的页面或功能。 在课程的实践部分,学员将有机会建立自己的Web应用程序。通过完成一系列的编程任务和项目,学员将应用所学的识,并将其运用到实际项目中。 总之,了课的Flask Web课程是一门全面而实践性强的课程,旨在帮助学员掌握使用Flask框架构建Web应用程序的技能。无论是对于想要从事Web开发的人来说,还是对于已经有一定经验的开发者来说,这门课程都将带来很大的收益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值