首页 文章 精选 留言 我的

精选列表

搜索[Java],共10000篇文章
优秀的个人博客,低调大师

Java多线程-线程中止

不正确的线程中止-Stop Stop:中止线程,并且清除监控器锁的信息,但是可能导致线程安全问题,JDK不建议用。Destroy: JDK未实现该方法。 /** * @author simon */ public class StopThread extends Thread { private int i = 0, j = 0; @Override public void run() { synchronized (this) { // 增加同步锁,确保线程安全 ++i; try { // 休眠10秒,模拟耗时操作 Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } ++j; } } /** * 打印i和j */ public void print() { System.out.println("i=" + i + " j=" + j); } } /** * @author simon * 示例3 - 线程stop强制性中止,破坏线程安全的示例 */ public class Demo { public static void main(String[] args) throws InterruptedException { StopThread thread = new StopThread(); thread.start(); // 休眠1秒,确保i变量自增成功 Thread.sleep(1000); // 暂停线程 thread.stop(); // 错误的终止 //thread.interrupt(); // @正确终止 while (thread.isAlive()) { // 确保线程已经终止 } // 输出结果 thread.print(); } } 理想状态:要么自增成功i=1, j=1,要么自增失败i=0, j=0真正程序执行结果:i=1, j=0 没有保证同步代码块里面数据的一致性,破坏了线程安全stop方法直接停止线程 正确的线程中止-interrupt 如果目标线程在调用Object class的wait()、wait(long)或wait(long, int)方法、join()、join(long, int)或sleep(long, int)方法时被阻塞,那么Interrupt会生效,该线程的中断状态将被清除,抛出InterruptedException异常。 如果目标线程是被I/O或者NIO中的Channel所阻塞,同样,I/O操作会被中断或者返回特殊异常值。达到终止线程的目的。 如果以上条件都不满足,则会设置此线程的中断状态。 对Demo中的示例,stop()改成interrupt()后,最终输出为"i=1 j=1",数据一致。 正确的线程中止-标志位 /** 通过状态位来判断 */ public class Demo4 extends Thread { public volatile static boolean flag = true; public static void main(String[] args) throws InterruptedException { new Thread(() -> { try { while (flag) { // 判断是否运行 System.out.println("程序运行中"); Thread.sleep(1000L); } } catch (InterruptedException e) { e.printStackTrace(); } }).start(); // 3秒之后,将状态标志改为False,代表不继续运行 Thread.sleep(3000L); flag = false; System.out.println("程序运行结束"); } } 在上方代码逻辑中,增加一个判断,用来控制线程执行的中止。

优秀的个人博客,低调大师

Java——小程序练习一

编写程序:由键盘输入给出一个百分制成绩,要求输出成绩等级’A’、’B’、’C’和’D’,90分以上为’A’,75~89为’B’,60~74为’C’,60分以下为’D’。 最开始写的方法:没有注意到百分制的限定,缺少条件分析,思考不到位。 public class Test01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); if (score>=90) { System.out.println("A"); }else if (score>=75 && score<=89) { System.out.println("B"); }else if (score>=60 && score<=74) { System.out.println("C"); }else if (score<60) { System.out.println("D"); } }} 改正后: public class Test01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); if (score>=90&&score<=100) { System.out.println("A"); }else if (score>=75 && score<=89) { System.out.println("B"); }else if (score>=60 && score<=74) { System.out.println("C"); }else if (score<60&&score>=0) { System.out.println("D"); }else{ System.out.println("很显然,这成绩是假滴!"); } }}

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册