In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要讲解了"Java常用的多线程实现方式是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java常用的多线程实现方式是什么"吧!
Thread简介
Thread 是一个类。Thread本身就实现了Runnable接口。它的声明如下:
public class Thread implements Runnable {}
Thread的作用是实现多线程。
ThreadTest.java 源码
class MyThread extends Thread{ private int ticket=10; public void run(){ for(int i=0;i0){ System.out.println(this.getName()+" 卖票:ticket"+this.ticket--); } } }};public class ThreadTest { public static void main(String[] args) { // 启动3个线程t1,t2,t3;每个线程各卖10张票! MyThread t1=new MyThread(); MyThread t2=new MyThread(); MyThread t3=new MyThread(); t1.start(); t2.start(); t3.start(); }}
运行结果
Thread-0 卖票:ticket10Thread-1 卖票:ticket10Thread-2 卖票:ticket10Thread-1 卖票:ticket9Thread-0 卖票:ticket9Thread-1 卖票:ticket8Thread-2 卖票:ticket9Thread-1 卖票:ticket7Thread-0 卖票:ticket8Thread-1 卖票:ticket6Thread-2 卖票:ticket8Thread-1 卖票:ticket5Thread-0 卖票:ticket7Thread-1 卖票:ticket4Thread-2 卖票:ticket7Thread-1 卖票:ticket3Thread-0 卖票:ticket6Thread-1 卖票:ticket2Thread-2 卖票:ticket6Thread-2 卖票:ticket5Thread-2 卖票:ticket4Thread-1 卖票:ticket1Thread-0 卖票:ticket5Thread-2 卖票:ticket3Thread-0 卖票:ticket4Thread-2 卖票:ticket2Thread-0 卖票:ticket3Thread-2 卖票:ticket1Thread-0 卖票:ticket2Thread-0 卖票:ticket1
Runnable简介
Runnable 是一个函数式接口,该接口中只包含了一个run()方法。它的定义如下:
@FunctionalInterfacepublic interface Runnable { public abstract void run();}
Runnable的作用,实现多线程。我们可以定义一个类A实现Runnable接口;然后,通过new Thread(new A())等方式新建线程。
RunnableTest.java 源码
class MyThread implements Runnable{ private int ticket=10; public void run(){ for(int i=0;i0){ System.out.println(Thread.currentThread().getName()+" 卖票:ticket"+this.ticket--); } } }};public class RunnableTest { public static void main(String[] args) { MyThread mt=new MyThread(); // 启动3个线程t1,t2,t3(它们共用一个Runnable对象),这3个线程一共卖10张票! Thread t1=new Thread(mt); Thread t2=new Thread(mt); Thread t3=new Thread(mt); t1.start(); t2.start(); t3.start(); }}
运行结果
Thread-0 卖票:ticket10Thread-2 卖票:ticket8Thread-1 卖票:ticket9Thread-2 卖票:ticket6Thread-0 卖票:ticket7Thread-2 卖票:ticket4Thread-1 卖票:ticket5Thread-2 卖票:ticket2Thread-0 卖票:ticket3Thread-1 卖票:ticket1
Thread和Runnable的异同点
Thread 和 Runnable 的相同点:都是"多线程的实现方式"。Thread 和 Runnable 的不同点:Thread 是类,而Runnable是接口;Thread本身是实现了Runnable接口的类。我们知道"一个类只能有一个父类,但是却能实现多个接口",因此Runnable具有更好的扩展性。此外,Runnable还可以用于"资源的共享"。即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。通常,建议通过"Runnable"实现多线程!
感谢各位的阅读,以上就是"Java常用的多线程实现方式是什么"的内容了,经过本文的学习后,相信大家对Java常用的多线程实现方式是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.