若米知识 > 百科 > 线程的几个方法

线程的几个方法

导读java中实现线程的方法有哪些优质回答以前在远标学过有三种:(1)继承Thread类,重写run函数创建:class xx extends Thread{public void run(){Thread.sleep(1000) //线程休眠1000毫秒,sleep使线程进入...

今天若米知识就给我们广大朋友来聊聊线程的三种方法,以下关于观点希望能帮助到您找到想要的答案。

java中实现线程的方法有哪些

java中实现线程的方法有哪些

优质回答以前在远标学过有三种:(1)继承Thread类,重写run函数

创建:

class xx extends Thread{

public void run(){

Thread.sleep(1000) //线程休眠1000毫秒,sleep使线程进入Block状态,并释放资源

}}

开启线程:

对象.start() //启动线程,run函数运行

(2)实现Runnable接口,重写run函数

开启线程:

Thread t = new Thread(对象) //创建线程对象

t.start()

(3)实现Callable接口,重写call函数

Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。

Callable和Runnable有几点不同:

①Callable规定的方法是call(),而Runnable规定的方法是run().

②Callable的任务执行后可返回值,而Runnable的任务是不能返回值的

③call()方法可抛出异常,而run()方法是不能抛出异常的。

④运行Callable任务可拿到一个Future对象,Future表示异步计算的结果。它提供了检查计算是否完成的方法,以等

待计算的完成,并检索计算的结果.通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果

java中有几种方法可以实现一个线程

优质回答4种。

1、匿名内部类形式。例:

Thread t = new Thread(new Runnable(){

//代码

});

t.start();

2、该类实现Runnablef接口。例子:

public class MyThread implements Runnable{

Thread t = new Thread(this);

t.start();

public void run(){ //启动线程自动调用此方法

}

}

3、其他类实现Runnable接口。例子:

public class MainThread{

Thread thread = new Thread(new GetThread());

thread.start();

}

class GetThread implements Runnable{

public void run(){ //启动线程自动调用此方法

}

}

4、内部类实现Runnable接口。例子:

public class ManClass{

Thread t = new Thread(new RunnableClass());

t.start();

class RunnableClass implements Runnable{

public void run(){ //启动线程自动调用此方法

}

}

}

多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么

优质回答java中多线程的实现方法有两种:1.直接继承thread类;2.实现runnable接口;同步的实现方法有五种:1.同步方法;2.同步代码块;3.使用特殊域变量(volatile)实现线程同步;4.使用重入锁实现线程同步;5.使用局部变量实现线程同步 。

其中多线程实现过程中需注意重写或者覆盖run()方法,而对于同步的实现方法中使用较常使用的是利用synchronized编写同步方法和代码块。

创建多线程有几种方法

优质回答1、通过继承Thread类创建线程

(1).首先定义一个类去继承Thread父类,重写父类中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。

(2).直接创建一个ThreadTest类的对象,也可以利用多态性,变量声明为父类的类型。

(3).调用start方法,线程启动,隐含的调用run()方法。

[java] view plain copy

public class ThreadTest extends Thread{

public void run(){

for(int i=0;i<=10;i++){

System.out.println(i);

}

}

public static void main(String[] args) {

ThreadTest thread1=new ThreadTest();

ThreadTest thread2=new ThreadTest();

thread1.start();

thread2.start();

}

}

2、通过实现Runnable接口创建线程

(1).定义一个类实现Runnable接口,重写接口中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。

(2).创建Runnable接口实现类的对象。

(3).创建一个ThreadTest类的对象,需要封装前面Runnable接口实现类的对象。(接口可以实现多继承)

(4).调用Thread对象的start()方法,启动线程

[java] view plain copy

public class ThreadTest implements Runnable{

@Override

public void run() {

for(int i=0;i<=10;i++){

System.out.println(i);

}

}

public static void main(String[] args) {

ThreadTest threadTest=new ThreadTest();

Thread theard=new Thread(threadTest);

theard.start();

}

}

3.通过Callable和Future创建线程

(1)创建Callable接口的实现类,并实现call()方法,该call()方法将作为线程执行体,并且有返回值。

(2)创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callable对象的call()方法的返回值。

(3)使用FutureTask对象作为Thread对象的target创建并启动新线程。

(4)调用FutureTask对象的get()方法来获得子线程执行结束后的返回值

[java] view plain copy

public class ThreadTest implements Callable<Integer>{

@Override

public Integer call() throws Exception {

int count =0;

for(int i=0;i<=10;i++){

count=count+i;

}

return count;

}

public static void main(String[] args) throws InterruptedException, ExecutionException {

ThreadTest test=new ThreadTest();

FutureTask<Integer> thread = new FutureTask<>(test);

new Thread(thread,"有返回值的线程").start();

System.out.println(thread.get());

}

}

使用实现Runnable接口方式创建线程可以共享同一个目标对象(TreadDemo1 tt=new TreadDemo1();),实现了多个相同线程处理同一份资源。

然后再看一段来自JDK的解释:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments calledrun.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable is implemented by classThread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassingThread. A class that implementsRunnable can run without subclassingThread by instantiating aThread instance and passing itself in as the target. In most cases, theRunnable interface should be used if you are only planning to override therun() method and no otherThread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

采用实现Runnable、Callable接口的方式创见多线程时,优势是:

线程类只是实现了Runnable接口或Callable接口,还可以继承其他类。

在这种方式下,多个线程可以共享同一个target对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU、代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。

劣势是:

编程稍微复杂,如果要访问当前线程,则必须使用Thread.currentThread()方法。

采用继承Thread类方式:

(1)优点:编写简单,如果需要访问当前线程,无需使用Thread.currentThread()方法,直接使用this,即可获得当前线程。

(2)缺点:因为线程类已经继承了Thread类,所以不能再继承其他的父类。

采用实现Runnable接口方式:

(1)优点:线程类只是实现了Runable接口,还可以继承其他的类。在这种方式下,可以多个线程共享同一个目标对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。

(2)缺点:编程稍微复杂,如果需要访问当前线程,必须使用Thread.currentThread()方法。

了解了上面的内容,相信你已经知道在面对线程的三种方法时,你应该怎么做了。如果你还需要更深入的认识,可以看看若米知识的其他内容。

本文来自网络,不代表本站立场,转载请注明出处:https://www.rm2g.com/baike/87817.html

作者: 若米知识

若米知识为您提供最全面的生活百科网站大全,主要为您提供数码、汽车、财经、美食、财经、科技、健康、教育、创业、电商、影视、百科等资讯信息,在这里可以找到您所需的答案,解决您所困惑的问题。
卡西欧手表价格多少钱~卡西欧5230手表价格
橡皮泥玫瑰花制作方法~用彩泥怎么做玫瑰花
联系我们

联系我们

0898-88881688

在线咨询: QQ交谈

邮箱: email@wangzhan.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部