multithreading

is invokeAll() a blocking call in java 7

六月ゝ 毕业季﹏ 提交于 2020-01-21 11:32:47
问题 ExecutorService executorService = Executors.newSingleThreadExecutor(); Set<Callable<String>> callables = new HashSet<Callable<String>>(); callables.add(new Callable<String>() { public String call() throws Exception { return "Task 1"; } }); callables.add(new Callable<String>() { public String call() throws Exception { return "Task 2"; } }); callables.add(new Callable<String>() { public String call() throws Exception { return "Task 3"; } }); List<Future<String>> futures = executorService

How does Thread.sleep() work when called from multiple threads

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-21 11:12:24
问题 sleep() is a static method of class Thread. How does it work when called from multiple threads. and how does it figure out the current thread of execution. ? or may be a more generic Question would be How are static methods called from different threads ? Won't there be any concurrency problems ? 回答1: how does it figure out the current thread of execution? It doesn't have to. It just calls the operating system, which always sleeps the thread that called it. 回答2: The sleep method sleeps the

How does Thread.sleep() work when called from multiple threads

北慕城南 提交于 2020-01-21 11:09:09
问题 sleep() is a static method of class Thread. How does it work when called from multiple threads. and how does it figure out the current thread of execution. ? or may be a more generic Question would be How are static methods called from different threads ? Won't there be any concurrency problems ? 回答1: how does it figure out the current thread of execution? It doesn't have to. It just calls the operating system, which always sleeps the thread that called it. 回答2: The sleep method sleeps the

How to view thread id of a process which has opened a socket connection?

一世执手 提交于 2020-01-21 10:57:13
问题 I have a process where multiple threads open multiple socket connection. I want to view this information and map what thread has opened which socket port. lsof -i and netstat command gives the process ID, but couldn't display thread id. Is there any command which prints this information? 回答1: As MarkR suggested, you need to use strace from startup: strace -fp <pid> The above command will show you per thread system calls like open(), read(), recv() etc, along with the descriptors used: [pid

How can I measure CPU time of a specific set of threads?

你离开我真会死。 提交于 2020-01-21 10:56:13
问题 I run C++ program in Linux. There are several threads pool (for computation, for io, for ... such things). The system call clock() gives me a way to measure the CPU time spent by all the CPU cores for the process. However, I want to measure the CPU time spent only by the threads in the computation threads pool. How can I achieve it? Thanks :D 回答1: To get CPU clock ID of every thread you can use: pthread_getcpuclockid and using this CPU clock ID you can retrieve the current thread CPU time

ObjectInputStream from socket.getInputStream()

谁都会走 提交于 2020-01-21 10:15:48
问题 I have server ServerSocket socketListener = new ServerSocket(Config.PORT); ... client = socketListener.accept(); and client sock = new Socket("127.0.0.1", Config.PORT); I want to transfer between them some serialized data using ObjectInputStream and ObjectOutputStream. When I try to do ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); Nothing happens neither on the server side nor client side. Everything falls on that line. Both the client and the server is

ObjectInputStream from socket.getInputStream()

二次信任 提交于 2020-01-21 10:15:05
问题 I have server ServerSocket socketListener = new ServerSocket(Config.PORT); ... client = socketListener.accept(); and client sock = new Socket("127.0.0.1", Config.PORT); I want to transfer between them some serialized data using ObjectInputStream and ObjectOutputStream. When I try to do ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); Nothing happens neither on the server side nor client side. Everything falls on that line. Both the client and the server is

non blocking client server chat application in java using nio

…衆ロ難τιáo~ 提交于 2020-01-21 09:11:19
问题 I built a simple chat application using nio channels. I am very much new to networking as well as threads. This application is for communicating with server (Server / Client chat application). My problem is that multiple clients are not supported by the server. How do I solve this problem? What's the bug in my code? public class Clientcore extends Thread { SelectionKey selkey=null; Selector sckt_manager=null; public void coreClient() { System.out.println("please enter the text");

How to dispatch_group_wait for dispatch_group_async inside an asynchronous block

笑着哭i 提交于 2020-01-21 08:38:09
问题 I have code that looks something like this: [SVProgressHUD show]; [imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, ...) { dispatch_group_async(queueGroup, queue, ^{ // Do stuff }); }]; dispatch_group_wait(queueGroup, DISPATCH_TIME_FOREVER); [SVProgressHUD dismiss]; Basically, display a loading animation HUD and start generating image thumbnails from an asset, then once it's done hide the HUD. I'm using a dispatch group since i want to make

Incrementing AtomicInteger in Java in 1000 threads does not generate value 1000 [duplicate]

ぃ、小莉子 提交于 2020-01-21 07:51:08
问题 This question already has answers here : Difference between shutdown and shutdownNow of Executor Service (3 answers) Closed 2 years ago . I am executing a java code where I have an AtomicInteger on which 1000 threads are trying to perform an incrementAndGet() . I was expecting the final value to be 1000. But each run is generating all sorts of different values. The code is as follows : class task implements Runnable { AtomicInteger i ; task(AtomicInteger ai) {i =ai ;} public void run() { i