The Java™ Tutorials — Concurrency :Defining and Starting a Thread 线程的定义和启动

The Java™ Tutorials — Concurrency :Defining and Starting a Thread 线程的定义和启动

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

关键点

  • 创建Thread的两种方式: 
    • 利用Runnable 
      • 样例:new Thread(new HelloRunnable())
    • 继承Thread 
      • 样例:class MyThread extends Thread {...}
  • 两种方式的选择: 
    • 第一种方式较为灵活常见,因为Runnable对象可以通过任何一个类实现,而不仅仅是Thread。 第二种则需要保证为Thread的子类

全文翻译

An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:

一个创建了线程实例的程序,必须提供将要在线程中执行的代码。这里有两种实现方法:

Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:

提供一个Runnable对象。这个Runnable接口仅声明了一个run()方法,这个方法负责装载于线程中执行的代码。Runnable对象会被传递到Thread的构造器中,正如HelloRunnable样例所示的那样:

public class HelloRunnable implements Runnable {

public void run() {
System.out.println("Hello from a thread!");
}

public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}

}

Subclass Thread. The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the HelloThread example:

继承Thread。Thread类自己实现了Runnable,虽然它的run方法什么也不做。一个应用可以去扩展Thread类,提供它自己的run实现,正如HelloThread样例所示的那样:


public class HelloThread extends Thread {

public void run() {
System.out.println("Hello from a thread!");
}

public static void main(String args[]) {
(new HelloThread()).start();
}

}

Notice that both examples invoke Thread.start in order to start the new thread.

注意,两个案例都调用了Thread.start方法以启动新创建的线程。

Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.

这两种风格应该如果选择?第一种,也就是采用了Runnable对象的那个,更常见。因为Runnable对象可以通过任何一个类实现,而不仅仅是Thread。第二种情况在简单程序中更加易用,但是受这样一个事实所限制——你的任务类必须为Thread的子类。本课时专注于第一种方案,也就是将Runnable任务从将要执行它的线程中分离出来。这种方法不仅仅更加易用,而且它更适用之后所写的高版本线程管理API。

The Thread class defines a number of methods useful for thread management. These include static methods, which provide information about, or affect the status of, the thread invoking the method. The other methods are invoked from other threads involved in managing the thread and Thread object. We’ll examine some of these methods in the following sections.

这个Thread类定义了一系列有助于线程管理的方法。其中包含一些静态方法。它们负责提供所在线程的相关信息,或者修改相关状态。其他方法由其他线程调用,包含了线程管理和Thread对象管理等功能。我们会在下面的章节里检验其中的部分方法。

基于MMSkeleton工具包中的ST-GCN模型实现一种基于动态拓扑图的人体骨架动作识别算法python源码+使用说明.zip 改进ST-GCN模型的骨架拓扑图构建部分,使用持续学习思想动态构建人体骨架拓扑图. 将具有多关系特性的人体骨架序列数据重新编码为关系三元组, 并基于长短期记忆网络, 通过解耦合的方式学习特征嵌入. 当处理新骨架关系三元组时, 使用部分更新机制 动态构建人体骨架拓扑图, 将拓扑图送入ST-GCN进行动作识别。 运行MMSKeleton工具包参考[GETTING_STARTED.md](./doc/GETTING_STARTED.md) - 单独使用ST-GCN模型进行人体动作识别参考[START_RECOGNITION.md](./doc/START_RECOGNITION.md) - 训练基于动态拓扑图的人体骨架动作识别算法 ``` shell cd DTG-SHR python ./mmskeleton/fewrel/test_lifelong_model.py ``` - 测试基于动态拓扑图的人体骨架动作识别算法 ``` shell cd DTG-SHR python ./mmskeleton/fewrel/train_lifelong_model.py ``` - 可视化算法运行结果 基于web server搭建前端 [[参考]](https://blog.csdn.net/gzq0723/article/details/113488110) 1、前端模块:包含 'static与'templates'文件夹为界面展示相关的代码。 templates里面包含了两个html的结构文档,用来定义浏览器的显示界面。 static里面的css和img用来修饰界面。 2、服务模块: servel.py里面是web服务的一个业务逻辑。 运行算法性能可视化web服务 ``` shell cd DTG-SHR python ./server.py ``` 【备注】 1、该资源内项目代码百分百可运行,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值