26
votes
5answers
5k views

Is this (Lock-Free) Queue Implementation Thread-Safe?

I was trying to create a lock-free queue implementation in Java, mainly for personal learning. The queue should be a general one, allowing any number of readers and/or writers concurrently. Would you ...
25
votes
2answers
2k views

Asynchronous network callback code

I did not get the job after submitting this piece of work in an interview, but I have no feedback to know what "BAD" things are inside this block of code. The requirements are: Connect to the ...
20
votes
3answers
1k views

Java method - levels of abstraction

In his Clean Code book, Robert C. Martin states that functions should do only one thing and one should not mix different levels of abstraction. So I started wondering whether the code below meets ...
12
votes
2answers
206 views

Can I make the thread safe case faster?

The code below is part of one of my projects called largetext, which actually stemmed from a question on StackOverflow. The goal is to provide access to a very large text file as a ...
11
votes
3answers
293 views

Extending ThreadPoolExecutor

I have implemented a ThreadPoolExecutor that will run a Consumer<T> only on elements not already consumed. This code uses ...
10
votes
5answers
268 views

Inner class calling outer class method

I created a console program that tests report results from JasperReport. It retrieves the list of reports, splits it, and passes each sublist to a new thread. Each thread executes its own reports and ...
10
votes
3answers
1k views

Concurrent programming java

I am creating a simple server that accepts a int and returns twice the value received. Here is my code: ...
10
votes
2answers
5k views

HTTP server and multi-threading optimization

I wrote an HTTP server for the management of scores for users and levels. It can return the highest score per level. It has a simple login with session-key. What do you think could be improved in ...
9
votes
2answers
339 views

Using synchronized blocks and volatile variables to ask a Java thread to terminate gracefully

Edit: Just to clarify, this isn't my original code, I've simplified it to make it smaller/easier to understand what I'm trying to do. It's my first time working with threads in Java, and I was ...
9
votes
1answer
168 views

Thread-Safe Integer Sequence

I would like to give an Integer to whatever demands it. For that I use the following code ...
8
votes
1answer
183 views

Cache<Long, BlockingDeque<Peer>> combined with Striped<ReadWriteLock>: is it thread-safe

I've implemented PeersContainer class with all operations that I need. I need something like multimap in concurrent environment, moreover I need remove entities ...
8
votes
3answers
9k views

Dining Philosophers problem code review

I've just finished my solution to the Dining Philosopher's Problem, but I am not confident with my code because I am still newbie to the concurrency world. I would appreciate it if you could leave me ...
8
votes
3answers
1k views

Please review this java synchronous queue implementation

I just want to make sure there aren't any deadlocks or inconsistencies (the code is also available on github). Disclaimer - In real life, I would not implement a queue myself, but use an existing ...
7
votes
2answers
112 views

How to make sure all the threads are getting consistent data without performance impacts?

I am working on a project in which I construct a url with a valid hostname (but not a blocked hostname) and then execute that URL using RestTemplate from my main ...
7
votes
1answer
319 views

Creating a thread for file transfer

I am creating an application in Java that runs at scheduled intervals and it transfer files from one server to another server. For SFTP I'm using ...
6
votes
4answers
89 views

Calculation of e performance issue

Can you give me some idea how to optimize the switch statements, maybe a for or do while ...
6
votes
2answers
604 views

A simple semaphore in Java

It is a simple semaphore in Java. Although it passes all their tests with flying colors, I'm still not sure it's correct. For example, the last thing I wasn't sure about was ...
6
votes
3answers
152 views

Is my below code thread safe w.r.t all the main application threads should get consistent data?

I am working on a project in which I construct a url with a valid hostname (but not a blocked hostname) and then execute that URL using RestTemplate from my main ...
6
votes
2answers
404 views

How to prevent reads before initialization is complete?

I am trying to implement lock by which I don't want to have reads from happening whenever I am doing a write. Below is my ClientData class in which I am using ...
6
votes
2answers
138 views

Implement HTTP Server with persistent connection

I am trying to implement a HTTP Server in Java, I want the server to use a persistent connection per thread for request and response. After some research on Google, this is how my program looks like. ...
6
votes
1answer
89 views

Simple chat console app

I wanted to practice using sockets and multithreading. This is simple code where I start a Server and connect to it via Client ...
6
votes
2answers
442 views

Multithreaded log test

I'm writng a logger extension that allows multiple threads to log a process and then dump that log to the main log in one atomic operation. The point of it is to make the logs easier to read when many ...
6
votes
1answer
141 views

Program to shutdown computer after a certain time

I'm trying to make a program that will shutdown my computer after a certain time that I will input via GUI. I'm wondering if I did it the correct way. Also, will this slow down my computer while it ...
6
votes
1answer
394 views

Ability to forget the memoized supplier value

Guava has a feature request which is asking to provide the ability to forget memoized value on a given supplier. On top on that I needed another functionality where if during the calculation of ...
5
votes
3answers
1k views

Client Sends XML, Server Parses XML, Queues Up and Executes Commands

I recently had a Java interview where I was asked to write a client-server application. I don't code primarily in Java, but with my preliminary Java knowledge, I thought I built a reasonable working ...
5
votes
3answers
271 views

Multiple background tasks

This is taken from my post at Stack Overflow here. I need your help to review my code for improvement and best practice. ...
5
votes
2answers
245 views

Multithreading correctly done?

I rarely write multithreaded code, and am on shaky ground when doing so. I make mistakes. I would like a sanity check of something I am going to introduce in one of my apps. There will be exactly ...
5
votes
1answer
90 views

Sending n requests in one time

I need to test my new functionality by sending about 100 requests in one time. ...
5
votes
4answers
4k views
5
votes
4answers
2k views

Optimizing a thread safe Java NIO / Serialization / FIFO Queue

I've written a thread safe, persistent FIFO for Serializable items. The reason for reinventing the wheel is that we simply can't afford any third party dependencies ...
5
votes
1answer
388 views

Socket handling in a thread

I am building an app for android to control VLC (mediaplayer) that runs on my computer. At the moment it's just a prototype that works, but I was wondering if I am correctly managing my HandlerThread ...
5
votes
1answer
176 views

Multi-threaded socket server high load

I'm trying to make a backend for QuizUp like application: user connects to a server, sends credentials and gets paired up with another user. After that server handles each pair, periodicaly sending ...
5
votes
2answers
2k views

Using an AsyncTask to populate a ListView in a Fragment From a SQLite table

This is the first time that I have played around with AsyncTask in Android and I wanted to make sure I'm using it correctly. The idea is I'm grabbing all the rows from a table in the database using ...
5
votes
2answers
696 views

Efficient way of having synchronous and asynchronous behavior in an application

I am working on a project in which I am supposed to make synchronous and asynchronous behavior of my client. In general, how our client will work as below - Customer will call our client with a ...
5
votes
1answer
58 views

Timing single operation to not be repeated for a fixed time

Sometimes "a" service does not respond and so we need to restart it. Usually it's a glitch in the network. We can have like 100 calls at the same time so the service cannot be restarted for 100 ...
5
votes
1answer
74 views

Using CountDownLatch for blocking the reads if writes are happening

I am trying to implement lock by which I want to avoid reads from happening whenever I am doing a write on my three maps. Requirements: Reads block until all three maps have been set for the first ...
5
votes
1answer
536 views

Multi threaded circular buffer

I should write a circular buffer with multithread write and single read ability. Here is my solution: ...
4
votes
5answers
506 views

Circular queue implementation

Implemented simple thread safe circular queue. Looking for code review, optimizations and best practices. ...
4
votes
2answers
104 views

Double checked locking 101

I've found a peace of code I've wrote not long ago. It is used to fetch some dictinary from DB table only once and then return it to requestors. Seems like I tryed to implement double-checked locking ...
4
votes
2answers
117 views

How many squares can you see?

With a positive outlook, I posted this challenge at Programming Puzzles & Code Golf. I tried to come up with my own solution to the challenge which is as follows: The output is displayed (not ...
4
votes
2answers
265 views

Using threads to find max in array

I really do not have access to anyone, outside of professors, that is highly knowledgeable about Java so I figured I would post my stuff here to further my knowledge and improve my coding. This is ...
4
votes
2answers
1k views

Cash-withdrawal from an ATM

I'm seeking review comments (design, performance, etc.) based on the problem statement: Write a CashWithDrawal function from an ATM which based on user specified amount dispenses bank notes. ...
4
votes
3answers
138 views

Usage of AtomicBoolean

I would like to find out if following usage of an AtomicBoolean as a flag variable is correct and good for a multi-threaded application. It is expected that data is ...
4
votes
2answers
221 views

Producer/Consumer implementation

I am using Netty embedded within Grails to process and display incoming SNMP messages. Since Netty 4 doesn't come with a built-in ChannelExecutionHandler I had to ...
4
votes
1answer
82 views

Pre-Java 5 threads

I would like to revise the fundamentals of Java threads on before-Java 5, so that I can understand improvements in Java 5 and beyond. I started off with a custom collection and need help on: Things ...
4
votes
1answer
641 views

Unit test an ExecutorService in a deamon

To execute tasks sequentially with a timeout I use ExecutorService.submit() and Future.get(). As I don't want to block calling ...
4
votes
1answer
139 views

Is this the best message delay algorithm?

In my application I am attempting to queue outgoing messages by the following rules By default they should be sent no less than messageDelay apart Some messages ...
4
votes
1answer
7k views

Dining Philosophers problem Solution with Java Reentrant Lock

I have implemented Dining Philosopher problem using ReentrantLock in java. The goal of this program is Every philosopher should follow the workflow of think,getchopsticks,eat,putchopsticks (No ...
4
votes
1answer
1k views

Simple multithreading task

I wrote program which simulates the work of the restaurant. It works but I would like to change it because I think there is better solution.Thanks for your suggestions. Client.java ...
4
votes
1answer
89 views

How to avoid reads before initialization is complete and return updated set of maps value?

This is a follow on to: How to prevent reads before initialization is complete? I am trying to implement lock by which I want to avoid reads from happening whenever I am doing a write on my three ...