首页 > 编程知识 正文

实现多线程的方式,java如何实现多线程

时间:2023-05-06 17:16:04 阅读:17422 作者:4346

1.Java多线程的实现方法有四种。

继承Thread类并重写run方法

2 .实现runnable接口,重写run方法,并将实现runnable接口的实现类的实例对象设置为Thread构造函数的target

通过Callable和FutureTask创建线程

4 .从线程池创建线程

2.Thread实现方式

继承Thread类,并调用run (重写方法,创建Thread对象并启动start ) )方法来启动线程。

publicclassthreaddemoextendsthread {

@Overridepublic voidrun () {int t=1; for(intI=0; i 10; I ) {

system.out.println (thread.current thread ().getName ) ) ' (t );

}

} publicstaticvoidmain (字符串[ ] args ) )

ThreadDemo td1=newThreadDemo (;

ThreadDemo td2=newThreadDemo (;

TD1.setname(thread1);

TD2.setname(thread2);

td1.start (;

td2.start (;

}

}

结果:

3.Runnable的实现方式

实现Runnable接口,调用run (实现方法,将接口实现类的实例作为Thread的target传递给带参数的Thread构造函数,然后启动线程)

publicclassrunnabledemoimplementsrunnable {

@Overridepublic voidrun () {int t=1; for(intI=0; i 10; I ) {

system.out.println (thread.current thread ().getName ) ) ' (t );

}

} publicstaticvoidmain (字符串[ ] args ) )

RunnableDemo rd=newRunnableDemo (;

threadtr1=newthread(rd;

threadtr2=newthread(rd;

tr1.setname(thread1);

tr2.setname(thread2);

tr1.start (;

tr2.start (;

}

}

}

结果:

3 .使用3.Callable和FutureTask创建线程实现方法

(1)编写Callable接口的实现类,实现Call方法;

)2)使用封装Callable对象的Call方法返回值的FutureTask类创建包装Callable对象的Callable实现类的实现。

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

)4) FutureTask对象的get ) ),获取子线程执行结束的返回值。

import Java.util.concurrent.callable; import Java.util.concurrent.execution exception; import Java.util.concurrent.future task; publicclasscallablefuturetaskdemoimplementscallable {

@ override公共integer call () Throwsexception ) intt=1; for(intI=0; i 10; I ) {

system.out.println (thread.current thread ().getName ) ) ' (t );

}返回;

} publicstaticvoidmain (字符串[ ] args ) )

callablecftd1=newcallablefuturetaskdemo (;

callablecftd2=newcallablefuturetaskdemo (;

futuretaskft1=newfuturetask (CFT D1 );

futuretaskft2=newfuturetask (CFT D2 );

threadt1=newthread(ft1;

threadT2=newthread(ft2;

T1.setname(thread1);

T2.setname(thread2);

t1 .开始(;

T2 .开始(; try{

system.out.println(ft1.get ) );

}互联互通{//todo auto-generated catch block

e .打印堆栈跟踪(;

} catch (执行任务) {//todo auto-generated catch block

e .打印堆栈跟踪(;

}

}

}

结果:

5 .如何实现线程池

import Java.util.concurrent.executorservice; import Java.util.concurrent.executors; publicclassexecutordemoimplementsrunnable {专用状态函数task _ num=2; //任务数

@Overridepublic voidrun () {int t=1; for(intI=0; i 10; I ) {

system.out.println (thread.current thread ().getName ) ) ' (t );

}

} publicstaticvoidmain (字符串[ ] args ) )

executorserviceexecutorservice=executors.newfixedthreadpool (3; for(intI=0; I

ExecutorDemo ed=newExecutorDemo (;

executorservice.execute(ed;

}

executorService.shutdown (;

}

}

结果:

java中线程池的顶级接口是执行程序。 Executor是运行线程而不是线程池的工具,真正的线程池是ExecutorService。

java上有哪个线程池?

1.newCachedThreadPool创建可缓存的线程池

2.newFixedThreadPool创建固定长度线程池

3.newScheduledThreadPool创建固定长度线程池

4.newSingleThreadExecutor创建单线程池

这里的例子使用的是newFixedThreadPool。

6 .总结

)1)实现Runnable接口比继承Thread类更有好处! Runnable接口适用于多个相同程序代码的线程处理相同的资源,避免了java中单继承限制,增强了程序健壮性,在多个线程中共享代码,实现了代码和数据的独立

)2)通过使用线程池,可以减少线程的创建和销毁次数,可以重用每个工作线程和执行多个任务,并可以根据系统的容错能力调整线程池中的工作线程数量

)3)在实际应用中,一个布尔标志位可以终止线程。

)4) ExecutorService、Callable均属于Executor框架。 返回结果的线程是JDK1.5中引入的新特征,Future接口也属于该框架,有了该特征获得返回值很方便。

执行Callable任务后,可以检索Future中的对象。 在此对象上调用get时,可以获取Callable任务返回的值(由通用确定)。 get ) )方法被阻止。 这意味着线程不返回结果,get方法继续等待。 此外,ExecutoreService还提供了通过传递Callable或Runnable返回Future的submit ()方法。 如果Executor后台线程池未完成Callable计算,则此调用将调用返回Future对象的get ()方法,并阻止它直到计算完成。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。