concurrent-programming

Is parallel programming == multithread programming?

点点圈 提交于 2019-11-30 10:56:56
问题 Is parallel programming == multithread programming? 回答1: Multithreaded programming is parallel, but parallel programming is not necessarily multithreaded. Unless the multithreading occurs on a single core, in which case it is only concurrent. 回答2: Not necessarily . You can distribute jobs between multiple processes and even multiple machines - I wouldn't class that as "multi-threaded" programming as each process may only use a single thread, but it's certainly parallel programming. Admittedly

How to solve the producer-consumer using semaphores?

扶醉桌前 提交于 2019-11-30 07:52:23
问题 I need to code a problem similar to producer-consumer, that must use semaphores. I tried a couple of solutions and none of them worked. First I tried a solution on Wikipedia and it didn't worked. My current code is something like that: Method run of the consumer: public void run() { int i=0; DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String s = new String(); while (1!=2){ Date datainicio = new Date(); String inicio=dateFormat.format(datainicio); try { Thread.sleep

Is parallel programming == multithread programming?

元气小坏坏 提交于 2019-11-29 22:49:47
Is parallel programming == multithread programming? Multithreaded programming is parallel, but parallel programming is not necessarily multithreaded. Unless the multithreading occurs on a single core, in which case it is only concurrent. Not necessarily . You can distribute jobs between multiple processes and even multiple machines - I wouldn't class that as "multi-threaded" programming as each process may only use a single thread, but it's certainly parallel programming. Admittedly you could then argue that with multiple processes there are multiple threads within the system as a whole...

Java: reference escape

丶灬走出姿态 提交于 2019-11-29 19:39:05
Read that the following code is an example of "unsafe construction" as it allows this reference to escape. I couldn't quite get how 'this' escapes. I am pretty new to the java world. Can any one help me understand this. public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } }); } } The example you have posted in your question comes from "Java Concurrency In Practice" by Brian Goetz et al. It is in section 3.2 "Publication and escape". I won't attempt to reproduce the details of that

How to solve the producer-consumer using semaphores?

做~自己de王妃 提交于 2019-11-29 05:22:14
I need to code a problem similar to producer-consumer, that must use semaphores. I tried a couple of solutions and none of them worked. First I tried a solution on Wikipedia and it didn't worked. My current code is something like that: Method run of the consumer: public void run() { int i=0; DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String s = new String(); while (1!=2){ Date datainicio = new Date(); String inicio=dateFormat.format(datainicio); try { Thread.sleep(1000);///10000 } catch (InterruptedException e) { System.out.println("Excecao InterruptedException

Java 8: Parallel FOR loop

时间秒杀一切 提交于 2019-11-28 18:06:15
I have heard Java 8 provides a lot of utilities regarding concurrent computing. Therefore I am wondering what is the simplest way to parallelise the given for loop? public static void main(String[] args) { Set<Server> servers = getServers(); Map<String, String> serverData = new ConcurrentHashMap<>(); for (Server server : servers) { String serverId = server.getIdentifier(); String data = server.fetchData(); serverData.put(serverId, data); } } Read up on streams , they're all the new rage. Pay especially close attention to the bit about parallelism: "Processing elements with an explicit for-loop

Java blocking issue: Why would JVM block threads in many different classes/methods?

荒凉一梦 提交于 2019-11-28 17:37:15
Update: This looks like a memory issue. A 3.8 Gb Hprof file indicated that the JVM was dumping-its-heap when this "blocking" occurred. Our operations team saw that the site wasn't responding, took a stack trace, then shut down the instance. I believe they shut down the site before the heap dump finished. The log had no errors/exceptions/evidence of problems--probably because the JVM was killed before it could generate an error message. Original Question We had a recent situation where the application appeared --to the end user--to hang. We got a stack trace before the application restart and I

How to explain the “deadlock” better?

拈花ヽ惹草 提交于 2019-11-28 17:15:00
I am struggling to explain "deadlock" in threads in easy words, so please help. What could be the best example of "deadlock" (say, in Java), and how it does happen in steps and how to prevent it? But without getting into details too deep. I know that's like asking two opposite things, but still. If you have any previous concurrent programming training experience -- it would be superb! Jack and Jill happens to want to make a sandwich at the same time. Both need a slice of bread, so they both goes to get the loaf of bread and a knife. Jack gets the knife first, while Jill gets the loaf of bread

Java: reference escape

房东的猫 提交于 2019-11-28 15:24:57
问题 Read that the following code is an example of "unsafe construction" as it allows this reference to escape. I couldn't quite get how 'this' escapes. I am pretty new to the java world. Can any one help me understand this. public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } }); } } 回答1: The example you have posted in your question comes from "Java Concurrency In Practice" by Brian Goetz

STL algorithms and concurrent programming

旧时模样 提交于 2019-11-28 05:04:04
Can any of STL algorithms/container operations like std::fill , std::transform be executed in parallel if I enable OpenMP for my compiler? I am working with MSVC 2008 at the moment. Or maybe there are other ways to make it concurrent? Thanks. Eugen Constantin Dinca There are a number of projects that aim at having parallel STL type libraries: OpenMP Multi-Threaded Template Library libstdc++ parallel HPC++ Parallel Standard Template Library Parallel Patterns Library (shamelessly borrowed from AshleysBrain's answer) To guarantee std::transform and std::fill to be parallel-safe, you will have to