线程优于可运行的优势 [英] Advantage of Thread over Runnable

查看:75
本文介绍了线程优于可运行的优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Java:实现Runnable的实现"与扩展线程"

Possible Duplicate:
Java: “implements Runnable” vs. “extends Thread”

1)为什么Java语言同时提供Thread和Runnable? 2)线程比可运行的有什么优势(为什么Java无法提供可运行的) 3)我们可以睡个好觉,给它一个id等吗?

1) Why does Java language provide both Thread and Runnable? 2) What are the advantages of thread over runnable ( why couldnt Java just provide a runnable) 3) Can we make a runnable sleep, give it an id etc?

推荐答案

1..线程是一个类,当您说.start()时,您将创建一个thread of execution 称为Runnable的run()方法,使它执行任务到 thread of execution,然后start()方法快速返回.

1. Thread is a class, and when you say .start() you create a thread of execution which is attached to an Instance of Thread Class. Therun() method of Runnable is called making it execute the task on to the thread of execution, and the start() method returns quickly.

2..Runnable是分配给newly created thread of executiontask.

3..因此,没有Thread类,您将无法运行Runnable.

这篇关于线程优于可运行的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆