java-threads

Java thread limit, JVM 9

痴心易碎 提交于 2019-12-11 08:59:17
问题 So according to most things I've read on the internet, the number of threads you can have in Java caps out around 10,000. However, in practice I can create nearly 500,000, at which point my computer becomes unresponsive. (The task manager goes a little funny - it starts claiming that though 99% of my 16 GB of memory is used, the highest-using program uses only ~300 MB. After everything stops responding, the fan quiets down, and the disk access light flashes only periodically, leading me to

How to build elastic search indexes for data stored in java application cache?

故事扮演 提交于 2019-12-11 08:03:44
问题 I have reader APIs written in Java that reads data from file system. Data is written to a file system by perl scripts. To attain better performance from reader APIs, I maintain an in-memory cache (basically a hashmap inside java application itself) for caching last 7 days of data from file system. For achieving this, whenever application starts, a dedicated thread takes care of reading last 7 days of data from file system and updating the cache. And I also created a thread that wakes up every

While loop not stopping despite condition value changing

心已入冬 提交于 2019-12-11 07:38:14
问题 I have the following lines of code in my main() function: The method Open() returns the value of a boolean variable open in the barber class. The run method in the barber class sets open variable to true but the while loop in the main function doesn't stop. It's like it's seeing barber.Open() as false forever, despite the change. This is the relative code in the barber class : 回答1: Declare the open field as volatile to ensure that write in one thread is visible to a reader in the other thread

execute thread periodically and stop it after condition is met or time is over

匆匆过客 提交于 2019-12-11 05:39:03
问题 I want to execute a thread periodically to check if some file is ready for uploaded and upload it as soon as it is ready then stop the thread immediately. Also, if a long time has passed I want to stop the thread regardless the file not being ready, but can't do it inside the run method itself. final ScheduledFuture<?> fileUploadedFuture = scheduler.scheduleAtFixedRate(() -> { try { if (fileReady("xyz.txt")) { uploadFile("xyz.txt") //cancel fileUploadedFuture and fileUploadedFutureCanceller }

Correct Way to update Observable List from background thread

巧了我就是萌 提交于 2019-12-11 01:56:46
问题 I'm trying to follow MVC for a test project, so my model should be completely independant from my view, however I'm not sure how I should update an Observable List which gets updated in a background thread (It's being given Strings about uploading files through FTP) so that the messages appear on the UI in a ListView. I am using JavaFX and trying to get my program as loosely coupled as possible. At this current moment, the GUI in the view package is depending on the fact that my model updates

Visual VM showing strange behavior

血红的双手。 提交于 2019-12-10 23:27:40
问题 I am using VisualVM to monitor my JBoss instance. I have attached a screenshot of it aswell. The problem is after I restart the JBoss instance, the CPU on the OS starts to go high. Load can go as high as 40 and JAVA process in top command shows upto 300% usage. This then goes on to slow down the application at the front end. VisualVM shows that CPU is high and that thread count is increasing also. How can I further go to the root cause of this ? Visual VM output - General 回答1: When it comes

how to give priority to the threads waiting in a semaphore?

自作多情 提交于 2019-12-10 06:40:55
问题 I have used a semaphore to restrict the number of threads accessing a function. I want the thread to be awakened next should be chosen by some priority which i will be giving,not by default way that semaphore awaken's them ? How can we achieve this ? Here is the implementation : class MyMathUtil2 implements Runnable { double a; double b; String name = "demo"; Thread t; //static int currentCount = 0; static int MAX_COUNT = 2; private final Semaphore available = new Semaphore(MAX_COUNT, true);

onSpinWait​() method of Thread class

送分小仙女□ 提交于 2019-12-09 14:01:51
问题 While learning Java 9 I came across a new method of Thread class, called onSpinWait​. According to javadocs, this method is used for this: Indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions on the part of other activities. Can someone help me understand this method giving a real-life example? 回答1: It's the same (and probably compiles to) as the x86 opcode PAUSE and equivalent the Win32 macro YieldProcessor , GCC's __mm_pause() and the C#

This thread program shows me different answers every time

风格不统一 提交于 2019-12-08 08:17:26
问题 This is a Java Program to Find The Number with Largest Divisors from 1-500000. public class Medium2 { static int count1 = 1; static int count2 = 1; static int big_count = 0; static int big = 0; Main method public static void main(String[] args) { Runnable runnable1 = new Runnable() { public void run() { The implementation goes here for (int num = 1; num <= 500000; num++) { for (int i = 2; i <= num; i++) { if (num % i == 0) { //Actual Logic count1++; } } if (count1 > big_count) { big_count =

This thread program shows me different answers every time

此生再无相见时 提交于 2019-12-08 05:26:26
This is a Java Program to Find The Number with Largest Divisors from 1-500000. public class Medium2 { static int count1 = 1; static int count2 = 1; static int big_count = 0; static int big = 0; Main method public static void main(String[] args) { Runnable runnable1 = new Runnable() { public void run() { The implementation goes here for (int num = 1; num <= 500000; num++) { for (int i = 2; i <= num; i++) { if (num % i == 0) { //Actual Logic count1++; } } if (count1 > big_count) { big_count = count1; //Number of Divisors big = num; //Largest Number } count1 = 1; } } }; And the thread execution