【Java语言】Java多线程一(线程启动线程中断)资料.doc

【Java语言】Java多线程一(线程启动线程中断)资料.doc

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
【Java 语言】Java 多线程 一 ( 线程启动 | 线程中断 ) 一. 线程启动 线程启动 : -- 1. 继承 Thread 运行线程 : 重写 Thread 类的 run 方法, 然后执行该线程; -- 2. 实现 Runnable 接口, 并运行线程; -- 代码示例 : [java] view plain copy 在CODE上查看代码片派生到我的代码片 package com.hanshuliang.thread; public class ThreadStart { public static void main(String[] args) { //1. 继承 Thread 运行线程 MyThread thread = new MyThread(); thread.start(); //2. 实现 Runnable 接口, 并运行线程 Thread runnableThread = new Thread(new MyRunnable()); runnableThread.start(); } //1. 继承 Thread 类 static class MyThread extends Thread{ @Override public void run() { super.run(); System.out.println(MyThread 线程启动); } } //2. 实现 Runnable 接口 static class MyRunnable implements Runnable{ @Override public void run() { System.out.println(MyRunnable 线程启动); } } } -- 运行结果 : [java] view plain copy 在CODE上查看代码片派生到我的代码片 MyThread 线程启动 MyRunnable 线程启动 三. 线程停止 线程停止常用方法 : -- 1. 使用 interrupt() 方法停止线程; -- 2. 使用退出标志, 让线程正常退出; -- 3. 弃用的方法 (不推荐) : 使用 stop() 方法强制停止线程, 但是该方法已经作废, 不建议使用; 1. 使用 interrupt() 方法停止线程 (1) 线程无法立即停止 interrupt() 使用说明 : -- 打标记 : 调用该方法, 不能马上停止该线程, 只是在当前线程打了一个停止标记; 代码示例 : -- 代码 : [java] view plain copy 在CODE上查看代码片派生到我的代码片 public class InterruptDemo { public static class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 1; i = 1000000; i++) //打印 一百万 数字, 大概持续 5 ~ 10 秒左右 System.out.println(i); } } public static void main(String[] args) throws InterruptedException { MyThread thread = new MyThread(); thread.start(); //启动线程 Thread.sleep(100); //启动线程 100ms 后中断线程 errupt(); //中断线程 } } -- 运行结果 : 这里只贴上最后几行 命令行的运行结果; [plain] vie

文档评论(0)

502992 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档