concurrency

Writing a thread safe modular counter in Java

Full disclaimer: this is not really a homework, but I tagged it as such because it is mostly a self-learning exercise rather than actually "for work". Let's say I want to write a simple thread safe modular counter in Java. That is, if the modulo M is 3, then the counter should cycle through 0, 1, 2, 0, 1, 2, … ad infinitum. Here's one...

C Socket Programming, 2 client queries at the same time

How can my client send two queries (in two different terminals) to the server at the same time? When i try it, only one works, the other closes socket. main () { readData (); int serverFd, clientFd, clientFd2,serverLen, clientLen; struct sockaddr_un serverAddress;/* Server address */ struct sockaddr_un clientAddress;...

Reading, incrementing and writing back a counter which is unique across multiple users (in EF 4)

I have a database table and a corresponding entity class (POCO with change tracking Proxy) with a member which acts as a counter: class MyEntity { public virtual int ID { get; set; } public virtual int Counter { get; set; } } There is a function in my web application which must fetch this entity by ID, read the Counter (which ...

Java Performance using Concurrency

How can I improve performance of this chunk of code ? What would be unit test case for the given problem statement ? Code: public class SlowDictionary { private final Map<String,String> dict = new HashMap<String,String>(); public synchronized String translate (String word) throws IllegalArgumentException {...

MailboxProcessor usage guidelines ?

Hi all, I've just discovered the MailboxProcessor in F# and it's usage as a "state machine" ... but I can't find much on the recommended usage of them. For example... say I'm making a simple game with 100 on-screen enemies should I use a MailboxProcessor to change enemy position and health; giving me 200 active MailboxProcessor? Is th...

Creating a thread for clocking and restart another thread

Hi! I have implemented the following class which keeps track of the time needed to execute an algorithm public class MyTimer extends Thread{ long timeElapsed = 0; public MyTimer(String parameters){ //initilise staff } pubic void run(){ long startTime = System.currentTimeMillis(); //DO SOME THING...

ThreadPoolExecutor Block When Queue Is Full?

I am trying to execute lots of tasks using a ThreadPoolExecutor. Below is a hypothetical example: def workQueue = new ArrayBlockingQueue<Runnable>(3, false) def threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.HOURS, workQueue) for(int i = 0; i < 100000; i++) threadPoolExecutor.execute(runnable) The problem is that I...

JVM consumes all CPU, most threads as BLOCKED. JVM bug?

Last night, a server (JBoss 5.1GA, Java(TM) SE Runtime Environment (build 1.6.0_20-b02), Java HotSpot(TM) 64-Bit Server VM, running in a Linux VM on VMWare) suddenly started to use 100% CPU. The app is a fairly typical J2EE business app running Seam, nothing particular about it. The load was extremely low at that time. I managed to get...

Java recurrent task, problem with the dates

Hi, I am trying to set a scheduled task in java for running once in a day. The problem is that it is running only in the first day. Any idea y? Thanks log.info("Schdualing midnight task"); Timer timer = new Timer(); Calendar date = Calendar.getInstance(); date.set(Calendar.HOUR_OF_DAY, 23); date.set(Calendar.MINUTE, 3...

How to load complex views concurrently

I have an Android activity that creates custom views, and it is an expensive process. I tried to use a Handler, but when I do, the progress dialog that shows, spins like once every 5 seconds. So I tried to use an AsyncTask. Now the progressDialog spins, but then it crashes, because it isn't allowed to load the UI? How can I do this witho...

Is Concurrent Haskell still restricted to a single OS thread?

In a 2005 research paper it said Concurrent Haskell is currently implemented only for a uni-processor. The runtime schedules lightweight Haskell thread within a single operating system thread. Haskell threads are only suspended at well-defined “safe points”; they cannot be pre-empted at arbitrary moments. Has this changed or is Co...

Are 64 bit assignments in Java atomic on a 32 bit machine?

If I have code like this - long x; x = 0xFFFFFFFFL; If i run this code on a 32 bit machine is it guaranteed to be atomic or is it possible that a different thread reading x, might get an incomplete/garbage value? ...

How do I run two python loops concurrently?

Suppose I have the following in Python # A loop for i in range(10000): Do Task A # B loop for i in range(10000): Do Task B How do I run these loops simultaneously in Python? ...

How to process n items in a collection concurrently

Hi, i am building a class that inherits from List. Items are going to be added to this collection at runtime and what i want is to have this class automatically do something with each block of n items after they have been added. So here is the scenario. 1] Create new class that inherits from List - CollectionX 2] At runtime we will be...

Lock or synchronized

Hi, I have main thread which calls another thread. timeout period of second one is 15 seconds. Current implementaion is main thread check second one for every 2 seconds. What I need to do is wait for only as long as second thread finish, upto a maximum of 15 seconds. I thought of trying wait and notify, but it will require synchronized....

Java program output - concurrent

It might be a silly question but, can the output of this program ( the way it is) be zero? public class Test2{ int a = 0; AtomicInteger b = new AtomicInteger(); public static Test2 c = new Test2(); public static void main(String[] args){ Thread t1 = new Thread(new MyTest1()); Thread t2 = new Thread (n...

Dear Java, why do most of the tutorials look as if they are leftovers of the 80's?

I learnt Java back in university. It's been 4 years since I last coded Java. I develop PHP applications mainly. This time I need a language with more powerful concurrency support. I thought to myself, I'll just revise my Java in an hour and I'm ready to go. As it turned out, there is no human friendly tutorials (!!) that can be easily f...

Haskell concurrency IO behavior

I am trying to understand concurrency in Haskell more deeply. I have the following code: import Control.Concurrent main :: IO () main = do arr <- return $ [1..9] t <- newMVar 1 forkIO (takeMVar t >> (print.show) arr >> putMVar t 1) forkIO (takeMVar t >> (print.show) arr >> putMVar t 1) forkIO (takeMVar t >...

java synchronized on method Not working?

I'm experimenting Java Multi-Threading using synchronization on method comparing with Atomic variables (java.util.concurrent.atomic package). Below are the classes: // Interface ICounter.java public interface ICounter { public void increment(); public void decrement(); public int value(); ...

Need a mix of Delayed and Map

I have an application without a save button; saving happens automatically in the background. As the user works with the app, tasks are created and put into a queue for execution. Some of them are delayed, for example, when you start typing, I wait 200ms before I update the corresponding value in the database. To make this more simple to...