- 浏览: 1111909 次
文章分类
- 全部博客 (379)
- S2SH (16)
- stuts2 (0)
- java语言 (81)
- JSP (17)
- <html>元素 (11)
- javaweb (4)
- web容器 (3)
- ext (23)
- javaScript (48)
- ant (1)
- liferay (1)
- sql (9)
- css (42)
- 浏览器设置 (3)
- office_world (1)
- eclipse (4)
- 其它 (28)
- 操作系统 (5)
- android (6)
- Struts2 (11)
- RegEx (3)
- mysql (5)
- BigDATA (1)
- Node.js (1)
- Algorithm (10)
- Apache Spark (1)
- 数据库 (5)
- linux (2)
- git (1)
- Adobe (3)
- java语言,WebSocket (1)
- Maven (3)
- SHELL (1)
- XML (2)
- 数学 (2)
- Python (2)
- Java_mysql (1)
- ReactJS (6)
- 养生 (4)
- Docker (1)
- Protocols (3)
- java8 (2)
- 书籍 (1)
- Gradle (2)
- AngularJS (5)
- SpringMVC (2)
- SOAP (1)
- BootstrapCSS (1)
- HTTP协议 (1)
- OAuth2 (1)
最新评论
-
Lixh1986:
Java并发编程:自己动手写一把可重入锁https://blo ...
Java之多线程之Lock与Condition -
Lixh1986:
http://win.51apps.com.cn/https: ...
temp -
ztwsl:
不错,支持很好
HttpServletRequest和ServletRequest的区别 -
guodongkai:
谢谢您能将知识精华汇编总结,让初学者们从原理中学会和提高。
javaScript之function定义 -
kangwen23:
谢谢了,顶顶
struts2中的ValueStack学习
一、问题背景
在使用多线程时,如果要开启一个任务,则就需要新建一个线程。
线程也是对象,那么是否可以不用新建,而使用原来的呢?
试试下面的方法:
结果报错了:
Exception in thread "main" java.lang.IllegalThreadStateException
原因:
线程在执行完 start() 方法后,就会自动销毁。
二、如何解决线程重用
1、分析
每一个 Thread 的类都有一个 start 方法。
Thread 的 start 方法是这样描述的:
Causes this thread to begin execution;
the Java Virtual Machine calls the run method of this thread.
启动线程。Java虚拟机会调用该类的 run 方法。
那么该类的 run() 方法中就是调用了 Runnable 对象的 run() 方法。
2、解决
我们可以继承重写 Thread 类,在其 start 方法中添加不断循环调用传递过来的 Runnable 对象。
这就是线程池的实现原理。循环方法中不断获取 Runnable 是用 Queue 实现的,在获取下一个 Runnable 之前可以是阻塞的。
三、问题出处
Question: java thread reuse
I have always read that creating threads is expensive. I also know that you cannot rerun a thread.
I see in the doc of Executors class: Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
Mind the word 'reuse'.
How do thread pools 'reuse' threads?
------------
Answer:
I think I understood what is confuzzabling you so here's my longer answer: the terminology is a tiny bit misleading (obviously, or you wouldn't ask that question specifically putting the emphasis on 'reuse'):
How do thread pools 'reuse' threads?
What is happening is that a single thread can be used to process several tasks (typically passed as Runnable, but this depend on your 'executor' framework: the default executors accepts Runnable, but you could write your own "executor" / thread-pool accepting something more complex than a Runnable [like, say, a CancellableRunnable]).
Now in the default ExecutorService implementation if a thread is somehow terminated while still in use, it is automatically replaced with a new thread, but this is not the 'reuse' they're talking about. There is no "reuse" in this case.
So it is true that you cannot call start() on a Java Thread twice but you can pass as many Runnable as you want to an executor and each Runnable's run() method shall be called once.
You can pass 30 Runnable to 5 Java Thread and each worker thread may be calling, for example, run() 6 times (practically there's not guarantee that you'll be executing exactly 6 Runnable per Thread but that is a detail).
In this example start() would have been called 6 times. Each one these 6 start() will call exactly once the run() method of each Thread:
From Thread.start() Javadoc:
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
BUT then inside each Thread's run() method Runnable shall be dequeued and the run() method of each Runnable is going to be called. So each thread can process several Runnable. That's what they refer to by "thread reuse".
One way to do your own thread pool is to use a blocking queue on to which you enqueue runnables and have each of your thread, once it's done processing the run() method of a Runnable, dequeue the next Runnable (or block) and run its run() method, then rinse and repeat.
I guess part of the confusion (and it is a bit confusing) comes from the fact that a Thread takes a Runnable and upon calling start() the Runnable 's run() method is called while the default thread pools also take Runnable.
-
Thread pool threads are basically running loops that pull submitted tasks off of a queue. The threads do not stop executing when they service a task, they just wait for the next one to be submitted to the queue. They never get 'rerun' as asked in the question, as they are just constantly running.
-
The run method of threads in a thread pool does not consist only of running a single task.
The run method of a thread in a thread pool contains a loop.
It pulls a task off of a queue, executes the task (which returns back to the loop when it is complete), and then gets the next task.
The run method doesn't complete until the thread is no longer needed.
Here is the run method of the Worker inner class in ThreadPoolExecutor.
-
It's not actually unnecessary - the (strange) test for task!=null in the loop makes it necessary to prevent continually processing the same task. Even if the loop were more conventional nulling task would be good because otherwise if getTask() blocks for a long time, the GC of task would otherwise be delayed for the same length of time.
-
-
http://stackoverflow.com/questions/2324030/java-thread-reuse
在使用多线程时,如果要开启一个任务,则就需要新建一个线程。
线程也是对象,那么是否可以不用新建,而使用原来的呢?
试试下面的方法:
Thread incT = new Thread(new Inc(c)); Thread decT = new Thread(new Dec(c)); for(int i= 0; i < 200; i++){ incT.start(); System.out.println("incT:" + incT.getName()); decT.start(); System.out.println("decT:" + decT.getName()); }
结果报错了:
Exception in thread "main" java.lang.IllegalThreadStateException
原因:
线程在执行完 start() 方法后,就会自动销毁。
二、如何解决线程重用
1、分析
每一个 Thread 的类都有一个 start 方法。
Thread 的 start 方法是这样描述的:
Causes this thread to begin execution;
the Java Virtual Machine calls the run method of this thread.
启动线程。Java虚拟机会调用该类的 run 方法。
那么该类的 run() 方法中就是调用了 Runnable 对象的 run() 方法。
2、解决
我们可以继承重写 Thread 类,在其 start 方法中添加不断循环调用传递过来的 Runnable 对象。
这就是线程池的实现原理。循环方法中不断获取 Runnable 是用 Queue 实现的,在获取下一个 Runnable 之前可以是阻塞的。
三、问题出处
Question: java thread reuse
I have always read that creating threads is expensive. I also know that you cannot rerun a thread.
I see in the doc of Executors class: Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
Mind the word 'reuse'.
How do thread pools 'reuse' threads?
------------
Answer:
I think I understood what is confuzzabling you so here's my longer answer: the terminology is a tiny bit misleading (obviously, or you wouldn't ask that question specifically putting the emphasis on 'reuse'):
How do thread pools 'reuse' threads?
What is happening is that a single thread can be used to process several tasks (typically passed as Runnable, but this depend on your 'executor' framework: the default executors accepts Runnable, but you could write your own "executor" / thread-pool accepting something more complex than a Runnable [like, say, a CancellableRunnable]).
Now in the default ExecutorService implementation if a thread is somehow terminated while still in use, it is automatically replaced with a new thread, but this is not the 'reuse' they're talking about. There is no "reuse" in this case.
So it is true that you cannot call start() on a Java Thread twice but you can pass as many Runnable as you want to an executor and each Runnable's run() method shall be called once.
You can pass 30 Runnable to 5 Java Thread and each worker thread may be calling, for example, run() 6 times (practically there's not guarantee that you'll be executing exactly 6 Runnable per Thread but that is a detail).
In this example start() would have been called 6 times. Each one these 6 start() will call exactly once the run() method of each Thread:
From Thread.start() Javadoc:
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
BUT then inside each Thread's run() method Runnable shall be dequeued and the run() method of each Runnable is going to be called. So each thread can process several Runnable. That's what they refer to by "thread reuse".
One way to do your own thread pool is to use a blocking queue on to which you enqueue runnables and have each of your thread, once it's done processing the run() method of a Runnable, dequeue the next Runnable (or block) and run its run() method, then rinse and repeat.
I guess part of the confusion (and it is a bit confusing) comes from the fact that a Thread takes a Runnable and upon calling start() the Runnable 's run() method is called while the default thread pools also take Runnable.
-
Thread pool threads are basically running loops that pull submitted tasks off of a queue. The threads do not stop executing when they service a task, they just wait for the next one to be submitted to the queue. They never get 'rerun' as asked in the question, as they are just constantly running.
-
The run method of threads in a thread pool does not consist only of running a single task.
The run method of a thread in a thread pool contains a loop.
It pulls a task off of a queue, executes the task (which returns back to the loop when it is complete), and then gets the next task.
The run method doesn't complete until the thread is no longer needed.
Here is the run method of the Worker inner class in ThreadPoolExecutor.
696: /** 697: * Main run loop 698: */ 699: public void run() { 700: try { 701: Runnable task = firstTask; 702: firstTask = null; 703: while (task != null || (task = getTask()) != null) { 704: runTask(task); 705: task = null; // unnecessary but can help GC 706: } 707: } finally { 708: workerDone(this); 709: } 710: }
-
It's not actually unnecessary - the (strange) test for task!=null in the loop makes it necessary to prevent continually processing the same task. Even if the loop were more conventional nulling task would be good because otherwise if getTask() blocks for a long time, the GC of task would otherwise be delayed for the same length of time.
-
-
http://stackoverflow.com/questions/2324030/java-thread-reuse
发表评论
-
java 将文件夹所有的文件合并到指定的文件夹下
2020-06-30 19:17 1060场景:将文件夹所有的文件合并到指定的文件夹下 另外:如果想效 ... -
多线程-线程池的四种创建方式
2020-04-01 18:38 486多线程-线程池的四种创建方式 https://blog.cs ... -
Java基础之:nio
2019-11-13 15:38 480一、理论讲解: 史上最强Java NIO入门:担心从入门到放弃 ... -
Java 分布式之:RPC 基本概念
2019-11-13 15:07 457转载: https://www.jianshu.com/p/ ... -
Java之 volatile 关键字原理详解
2019-11-07 15:36 545一、什么是 volatile ? ... -
POI实现excell批注背景图片(仿html浮窗显示图片)
2019-10-21 08:17 684POI实现excell批注背景图片(仿html浮窗显示图片) ... -
Java之设计模式之 Observer 观察者
2019-07-04 17:21 1066观察者设计模式 Java 已经实现了该模式,并且提供了使用类 ... -
HashMap, LinkedHashMap and TreeMap
2019-03-01 11:04 676https://stackoverflow.com/a/177 ... -
Java lib 操作 excel 插入图片
2019-01-19 12:46 880https://poi.apache.org/componen ... -
数据库连接池C3P0
2018-05-29 16:50 892一、名字的由来 很多 ... -
Java8之集合(Collection)遍历 forEach()、stream()
2018-05-29 14:39 20748package java8.collections; ... -
Junit Vs main on "java.util.concurrent.Executors"
2017-11-10 16:44 810Same code with different result ... -
Java之大数据学习路线
2017-11-03 10:08 5721三个月大数据研发学习 ... -
Java中创建对象的5种方式
2017-10-26 14:21 842一、Java之5种创建对象的方式 ————————————— ... -
Log4j和Slf4j的比较
2017-06-23 12:41 1409一直搞不清 Log4j 和 SLF4j 的关系。今天才若有所 ... -
Java之Java7新特性之try资源句式
2017-04-20 14:58 5385Java之Java7新特性之try资源句式 一、【try资源 ... -
Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future
2017-03-04 21:27 3834一、如何使用 ExecutorService.submit() ... -
Java之 java.util.concurrent 包之Executor与ExecutorService
2017-03-04 21:18 2703一、问题: execute() 与 submit() 的区别? ... -
JAVAEE之单用户登录
2017-02-05 11:55 1058单用户登录是系统中数据一直性的解决方案之一。 问题背景: 试 ... -
Java之语法之方法调用之地址传值之空对象(null)
2017-01-26 14:05 3317一、问题: public void test ...
相关推荐
Java 多线程教程之如何使用线程池详解 Java 多线程教程中,线程池是一种非常重要的概念。在 Java 中,线程池是指一个线程的集合,可以重复使用这些线程来执行任务。使用线程池可以提高服务器的性能和可扩展性。本文...
2. 提高系统的性能:线程池可以重复使用已经创建的线程,避免了频繁地创建和销毁线程,从而提高系统的性能。 线程池的组成部分 线程池包括以下四个基本组成部分: 1. 线程池管理器(ThreadPool):用于创建并管理...
在 Java 中,线程是程序执行的独立路径,它们允许应用程序并发执行多个任务。线程池则是一种管理线程的机制,它可以帮助我们更高效、更可控地使用线程资源。下面将详细介绍 Java 中线程的创建方式以及线程池的使用。...
### Java线程池详解 ...Java线程池是一种高效、实用的多线程处理方案,它通过管理和复用一定数量的线程来避免频繁创建和销毁线程所带来的开销。正确配置和使用线程池对于提升程序的性能和稳定性具有重要意义。
- **固定大小的线程池**:`newFixedThreadPool(int nThreads)`创建一个可重用的固定线程数量的线程池,这些线程按需创建,且可以重复使用。 ```java ExecutorService service = Executors.newFixedThreadPool(3);...
总之,Java多线程是Java程序员必须掌握的核心技能之一,它涉及到程序的并发性、性能优化以及资源管理等多个方面。通过"java_Thread.txt"这样的实例学习,可以帮助我们更好地理解和应用Java的多线程特性,提升我们的...
线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池可以有效地控制运行的线程数量,当线程数量过多时,可以适当控制新的线程的创建,避免过多线程导致系统资源的...
Java多线程技术是Java编程语言中的一个重要部分,它允许程序员创建多个线程来执行多个任务,这样可以有效地利用系统资源,提高程序的执行效率。在Java中,多线程的实现主要有两种方式:一种是继承Thread类,另一种是...
线程池是一种用于管理线程的技术,它可以重复使用预先创建的线程,而不是每次需要执行新任务时都创建新的线程。Java中的`ExecutorService`接口提供了创建和管理线程池的功能。 ##### 5.1 创建线程池 使用`Executors...
Java多线程编程是Java平台的核心特性之一,特别是在Java 5之后,其并发包`java.util.concurrent`的引入,极大地提升了开发多线程应用的效率和性能。本篇内容主要探讨了如何使用Java 5的并发工具来实现一个高效的网络...
#### 十一、实战Java多线程编程精要之高级支持 - **线程组**: - 用于管理和控制一组线程。 - **线程间发信**: - 实现线程间的通信。 - **屏蔽同步**: - 采用特定技术减少同步的使用。 - **守护线程**: - 守护...
线程池预先创建一定数量的线程,这些线程可以重复使用。当有新的任务提交给线程池时,线程池会从空闲线程中选择一个执行该任务。 ```java ExecutorService executor = Executors.newFixedThreadPool(5); executor....
- **线程池**:通过使用线程池,可以重复使用预创建的线程,减少创建和销毁线程的开销。常见的线程池类型包括`FixedThreadPool`、`CachedThreadPool`等。 #### 六、线程间的通信与同步 - **wait()、notify() 和 ...
线程池和阻塞队列是实现并发的关键组件之一。本文将详细介绍线程池原理、使用场景及注意事项,以及阻塞队列的相关知识。 首先,线程池是一种基于池化思想管理线程的技术,它可以重用一组线程执行多个任务。线程池的...
### Java多线程几个核心概念 #### 一、理解多线程 多线程是一种让程序内部的不同...总之,Java多线程是开发高性能应用程序的基础之一。掌握多线程的基本概念和技术,对于提高程序的并发能力和响应速度具有重要意义。
从给定的信息来看,文档标题和描述均指出是关于“Java多线程设计模式”的内容,但是提供的部分内容并未涉及实际的技术细节,而是重复了联系方式。因此,本篇将基于标题和描述来展开“Java多线程设计模式”的知识点...
2. **多线程**:多线程是Java的核心特性之一,它允许程序同时执行多个任务。在爬虫项目中,多线程可以提高抓取网页的效率,每个线程负责处理不同的URL,加快整体爬取速度。 3. **网络编程**:Java的`java.net`包...
2. **死锁**:多线程程序中最常见的问题之一。 - **预防措施**: - 避免嵌套锁定。 - 使用锁顺序或锁所有权的概念。 3. **饥饿**:某些线程可能因为优先级较低而始终无法获得执行机会。 - **解决方法**:合理...
线程池是Java并发编程中一个重要的概念,它是一种多线程处理形式,通过维护一组可重用线程来减少创建和销毁线程的开销。线程池的使用可以有效控制运行的线程数量,避免因频繁创建和销毁线程而带来的性能损失。 1. ...
### 并发编程之线程池的优雅使用 #### 基本概念 在深入了解线程池之前,我们首先需要了解几个基本的概念。 - **进程**:是指正在运行的一个程序的实例,它拥有独立的地址空间,每个进程都有自己的状态(如运行、...