markdown
### 概览
```text
#Thread定义:
public class Thread extends Object implements Runnable
# 文字创建方法
There are two ways to create a new thread of execution.
* One is to declare a class to be a subclass of Thread. This subclass should override the run
method of class Thread. An instance of the subclass can then be allocated and started.
* The other way to create a thread is to declare a class that implements the Runnable interface.
That class then implements the run method.An instance of the class can then be allocated,passed
as an argument when creating Thread, and started.
```
*
*From Java Docs*
* ### 示例代码 #### (一)extends Thread ```text class ThreadTestOne extends Thread{ @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("hello world " + i); } } } #调用 ThreadTestOne threadTest = new ThreadTestOne(); threadTest.start(); ``` * 注意 ```text # 若是直接使用threadTest.run() 实际上是在main进程中顺序调用run()方法 # 再次启动threadTest线程,必须重新建立新对象,原因在于threadStatus在源码中asset为 0 ``` #### (二)implements Runnable ```text class ThreadTestTwo implements Runnable{ @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("hello world two" + i); } } } #调用 ThreadTestTwo threadTestTwo = new ThreadTestTwo(); new Thread(threadTestTwo).start(); ``` * 注意 ```text # threadTestTwo.run()实际上是调用了Runnable类型的target的run() ``` ### ThreadFactory ##### (一)定义接口 ```text #public interface ThreadFactory #实现ThreadFactory接口 class SimpleThreadFactory implements ThreadFactory { #重写newThread方法 @Override public Thread newThread(Runnable runnable) { return new Thread(runnable); } } ``` ##### (二)使用接口 ```text new SimpleThreadFactory().newThread(new ThreadTestTwo()).start(); ``` ### IDEA 启动退出 **记录日期20/03/23*
* ```text 最新 2023.1版本IDEA 无法适配 223.228版本中文语言包 需要IDEA版本回滚至2022.3.3 ``` **未完待续*
* **[返回教程主页](https://www.monody.net/p/blog-page_3.html)*
*
评论