java-threads

maximum number of threads allowed to run

自古美人都是妖i 提交于 2019-12-08 04:44:43
问题 Maximum number of threads in my linux system is 93946. I got this value from cat /proc/sys/kernel/threads-max . But when I create around 32768 threads I got error cannot create new native threads How can I create maximum number of threads(around 93946)? If it is not possible to create maximum number of threads (93946), how can I find maximum number of threads allowed to run? 回答1: You can find max thread with command: sysctl -a| grep kernel.threads-max Edit the /etc/sysctl.conf file and add

Print odd and even using two threads in Java

早过忘川 提交于 2019-12-06 14:59:46
I am trying to do it using two threads like below. Can someone point the obvious mistake I am doing here? public class OddEven { public static boolean available = false; public static Queue<Integer> queue = new LinkedList<Integer>(); static Thread threadEven = new Thread() { @Override public void run() { printEven(); } public synchronized void printEven() { while (!available) { try { wait(); Thread.sleep(2000); } catch (InterruptedException e) { } } System.out.println(queue.remove()); available = false; notifyAll(); } }; static Thread threadOdd = new Thread() { @Override public void run() {

Parallel programming with threads in Java

断了今生、忘了曾经 提交于 2019-12-06 07:39:24
问题 Parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework . Let's say in Java, using ExecutorService I create a thread pool of say 4 threads and submit to it say 10 tasks which means 4 threads will start executing the 4 tasks and other 6 tasks will be picked up by threads as and when any thread finishes its task. Let's assume I have a quad processor having 4 cores, I know a thread can be run on a single core(not taking hyper-threading concept here) so

Divide an uneven number between Threads

坚强是说给别人听的谎言 提交于 2019-12-06 00:29:11
问题 I am just learning Threads in Java and I want to sort a list of words alphabetical. My program read the words of a txt-file and put them in a String-array. The user can choose how many threads they want to use themselves. I want to split the array in even (as possible) chunks that the threads can sort by themselves. So to my question: How can I split the array.length as even as possible across the threads? My mind is blanking and I can't think of a smart way to do this. e.g: If I have a array

Is there a stack space for every thread?

吃可爱长大的小学妹 提交于 2019-12-05 01:38:08
If I understand correctly the stack is for local primities and references to the objects in the heap. So what happens if you have more than one threads? Do they share the same stack space at the same time (but different areas) or does the JRE switch contexts and load-deload the stack content when switching between threads? Or does the JRE allocate separate stacks for each threads? Or does the JRE allocate separate stacks for each threads? Conceptually yes. (See this JVM spec link , for example.) How the spec's conceptualization gets implemented in a particular JVM is ... implementation

Parallel programming with threads in Java

我与影子孤独终老i 提交于 2019-12-04 13:38:35
Parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework . Let's say in Java, using ExecutorService I create a thread pool of say 4 threads and submit to it say 10 tasks which means 4 threads will start executing the 4 tasks and other 6 tasks will be picked up by threads as and when any thread finishes its task. Let's assume I have a quad processor having 4 cores, I know a thread can be run on a single core(not taking hyper-threading concept here) so will all my 4 threads work in parallel, 1 thread running on 1 core? Isn't it a parallel programming?

Change javafx circles color in certain time using multithreads

六眼飞鱼酱① 提交于 2019-12-04 06:19:28
问题 I am creating traffic light simulator with javafx that changes colour in every 2 seconds (first red light blinks and remain for 2 seconds) using the concept of multithreading. I have my code as given below. But it doesn't work as expected. It just blinks all light and sto Can somebody help me figure it out where I went wrong? Thanks public void startThreads() throws InterruptedException { Runnable taskOne = new Runnable(){ @Override public void run(){ try { Platform.runLater(new Runnable() {

Divide an uneven number between Threads

吃可爱长大的小学妹 提交于 2019-12-04 05:18:04
I am just learning Threads in Java and I want to sort a list of words alphabetical. My program read the words of a txt-file and put them in a String-array. The user can choose how many threads they want to use themselves. I want to split the array in even (as possible) chunks that the threads can sort by themselves. So to my question: How can I split the array.length as even as possible across the threads? My mind is blanking and I can't think of a smart way to do this. e.g: If I have a array.length of 22 and 4 threads, how can give the threads in this case; 6, 6, 5 and 5 sized pieces of the

How notify method works

此生再无相见时 提交于 2019-12-04 04:47:51
问题 As per the javadoc notify Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods. I want to know how notify achieve this behavior. On many sites I read it sends a signal but What does signal means here? Does notify sends a signal directly to first waiting

onSpinWait​() method of Thread class

南笙酒味 提交于 2019-12-03 22:08:56
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? 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# method Thread.SpinWait It's a very weakened form of yielding: it tells your CPU that you are in a loop that