Java 如何让一个线程运行特定的时间,然后优雅的终止?
伊谢尔伦
伊谢尔伦 2017-04-17 13:21:02
0
4
398

有一个对象列表,按对象的过期时间戳升序排序,当内存快要不足时,想触发一个线程从头遍历列表,把列表前面过期的对象都释放掉(对象的资源是被pool的,必须手动release)

列表不是线程安全的(因为程序其他部分逻辑的原因,这里使用线程安全的集合类并不能解决问题),工作线程和释放资源的线程同时只能有一个操作对象列表

我的想法是,如果释放资源的线程在列表上操作太长时间,工作线程一直被阻塞,很长时间整个系统就不能提供服务了,所以我想限制每次释放资源的线程的执行时间,在给定的时间里能释放多少对象就释放多少(并且时间结束时,是优雅的结束释放工作),然后工作线程继续执行

请问如何实现我这个思路?如果同时只有一个线程访问对象列表,是不是把释放对象的工作放在工作线程中更好?怎么实现执行特定的时间呢?希望能有更标准的思路

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
Ty80

Worker threads and threads that release resources can only have one operation object list at the same time

This premise determines that you must use synchronized blocks to protect the logic of operating this list. This goes without saying;
The remaining question is: How to make the recycling thread take less time to operate the list?

My idea is this: It's very simple. When your recycling thread operates the list, it only does one thing: move the recycled object out of the list and put it in another place (such as another list, "List 2")

If you just "move out", this operation is very fast. I don't know how big your list is, but it can usually be completed instantly;

Then let the working thread operate the list, and the recycling thread slowly handles the recycling work in "List 2" without interfering with each other;

If your object list is really extremely large, then you can design the recycling thread into a "primary-secondary" thread 2-layer structure, in which the main thread starts the sub-thread to remove the list and holds the sub-thread object Application;
The child thread implements the interrupt method and throws a custom exception in the method;

After starting the sub-thread, the main thread sleeps (for a time you think is appropriate). When it wakes up, if it finds that the sub-thread has not ended yet, it calls the interrupt method of the sub-thread

刘奇

If you simply release it, it shouldn't be a time-consuming operation. Besides, when JVM garbage collection stops the world, it is inevitable that your logic will be blocked. If your quantity is indeed huge, then you can control the number of recycling items each time to make the congestion time as short as possible. Another is to limit the number of object pools in the program, because if you don't limit it, even with the best algorithm, the program will easily get out of control when the number of objects increases.

大家讲道理

If the release operation is a very lightweight operation, that is, the time to release each object is very short, you can write a loop in the release thread to determine the current execution time and release one by one, similar to this:

codepublic void run() {
    while(在给定的时间) {
        release(object);
    }
}
黄舟

I think the people above are right, releasing is a light operation. If you are really worried about blocking time, I have an idea.
Because your object list (assumed to be called ol) is not thread-safe, every operation will lock ol.
Then you can open a separate daemon thread to poll the jvm memory. Once the memory reaches this threshold, it can be considered to be almost used up, and then mark a global change as true.
When your worker thread discovers that this global variable is true, before saving the new object to ol, save the first few objects of ol (1 or 2, not more) because the polling thread does not find other work before the memory is restored. The thread will still release) release and then save the new one.
The daemon thread that polls the memory sets the global variable to false after finding that the memory is lower than the specified threshold, so that the new worker thread will not release the memory.
In this case, you have not introduced any threads that need to lock OL at all, and the blocking rate of your OL is still your original working thread. Since the polling memory thread is a daemon thread, you don't have to worry about graceful termination.

Update

Suddenly I realized that you asked about how to let the thread run for a specified time. . . .
Note the start time before the thread is stuck in the while loop. Calculate the time consumption based on the start time in your loop. You can jump out or sleep when the time you specify is reached.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!