Tagged Questions
9
votes
1answer
160 views
Thread-Safe Integer Sequence
I would like to give an Integer to whatever demands it. For that I use the following code
...
1
vote
0answers
15 views
Mutualising multi-threaded calls to EJB from Servlet
I have a Servlet that makes an EJB call to a backing server which takes about a second to return some data that changes reasonably regularly.
I can't cache the data on the Servlet-side, so I have ...
6
votes
2answers
593 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
4answers
88 views
Calculation of e performance issue
Can you give me some idea how to optimize the switch statements, maybe a for or do while ...
0
votes
0answers
7 views
In which object should I implement wait()/notify()? [migrated]
I'm working in an Android project with multithreading. Basically I have to wait to the server to respond before sending more data.
The data sending task is delimited by the flag ...
9
votes
2answers
323 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 ...
4
votes
5answers
499 views
Circular queue implementation
Implemented simple thread safe circular queue. Looking for code review, optimizations and best practices.
...
5
votes
3answers
259 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.
...
3
votes
1answer
54 views
Sequential Thread Execution using wait() and notifyAll()
Problem Statement:
T1, T2 to Tn threads prints numbers up to N such that each threads
prints number in circular sequential fashion. T1 prints 1, T2 prints
2, T3 prints 3 and T1 prints 4 again ...
6
votes
3answers
148 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 ...
7
votes
2answers
107 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 ...
0
votes
2answers
88 views
Continually process results of multi-thread worker [closed]
Let's say we've got some SQL queries and want to display the results. Processing takes some time, so I do it multi-threaded.
The following works quite well for me. I reduced the code to the important ...
6
votes
2answers
128 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. ...
4
votes
3answers
125 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 ...
6
votes
1answer
127 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 ...
5
votes
1answer
71 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 ...
3
votes
1answer
37 views
Should I worry about spurious wakeups in Java's BlockingQueue.take()?
I wrote this code where I trap InterruptedException's in a blocking queue and try again to consume from the same queue thinking of Spurious wakeups. I don't know if ...
4
votes
1answer
86 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 ...
6
votes
2answers
398 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 ...
12
votes
2answers
205 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 ...
5
votes
1answer
162 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 ...
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 ...
11
votes
3answers
288 views
Extending ThreadPoolExecutor
I have implemented a ThreadPoolExecutor that will run a Consumer<T> only on elements not already consumed. This code uses ...
4
votes
2answers
114 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 ...
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 ...
10
votes
5answers
260 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 ...
4
votes
2answers
242 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 ...
8
votes
1answer
182 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 ...
5
votes
1answer
89 views
Sending n requests in one time
I need to test my new functionality by sending about 100 requests in one time.
...
4
votes
2answers
214 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 ...
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 ...
2
votes
1answer
166 views
Filter email with JavaMail
I'm trying to develop code to filter my e-mail with JavaMail, but I doubt it's the best way of doing this. And I want to collect all messages without attachment.
...
7
votes
1answer
287 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 ...
1
vote
2answers
104 views
Is that correct example of unsafe publication in java? [closed]
There are many examples of unsafe publication on the web. But I cannot find a complete program for reproducing it.
I wrote an example but I do not sure whether it correct or not.
And in case of ...
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 ...
3
votes
1answer
251 views
How to avoid GSON deserialization if I am aware of error response from the server?
I am working on a project in which I am making a call to one of my servers using RestTemplate which is running a restful service and getting the response back from ...
3
votes
1answer
171 views
How to make builder pattern thread safe in the multithreading environment?
I am working on a project in which I need to have synchronous and asynchronous method of my java client. Some customer will call synchronous and some customer will call asynchronous method of my java ...
4
votes
2answers
101 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 ...
3
votes
3answers
851 views
Decrease CPU usage in while loop
I've a application which runs with two threads. The main thread runs in one infinity-while-loop:
...
5
votes
2answers
614 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 ...
2
votes
1answer
523 views
Multithreaded horse race simulation
I'm just starting to get along with threads in Java and I need some code review,
about interfaces, classes, managing threads, correct writing, secure threads, exposed objects etc.
Horse-Race ...
2
votes
1answer
1k views
Thread safe enqueue and dequeue method in Queue
As an exercise, I have implemented a linked queue whose enqueue and dequeue methods are synchronized. There is a ...
1
vote
1answer
351 views
Background image loader AsyncTask
Usually, AsyncTasks are meant to be one-shot, i.e. you start it, it fetches some data, displays the result and dies. However, I'm now using a long-running ...
1
vote
2answers
654 views
Producer-Consumer using low level synchronization
I saw a tutorial from a site where it showed the use of ArrayBlockingQueue as a thread-safe concurrent data-structure . Just for learning purposes , i tried to ...
1
vote
3answers
128 views
Optimize calculation of distances between pairs of points
I was trying to solve a challenge. The execution time should not exceed 5s, but for some value my code took longer. I want to know if I can optimize my code for performance.
...
3
votes
2answers
231 views
Java Bouncing Ball Review
Just looking for some help reviewing/refactoring. For example, could the classes random/vertical be refactored into 1 class instead of 2?
BouncingBalls.java
...
1
vote
3answers
175 views
Bouncing ball program [closed]
This is a typical bouncing ball program. I'm looking to improve and perhaps add to the program. For example, is it possible to be able to click on a ball and have it pause?
...
1
vote
1answer
186 views
Calling different bundles in parallel using ExecutorService
I am working on a project in which I will be having different Bundles. Let's take an example, Suppose I have 5 Bundles and each of those bundles will have a method name ...
2
votes
1answer
141 views
is this producer-consumer implementation correct?
I want to ensure that, as much as possible given my skill level, that I'm using relevant patterns correctly and following naming conventions. This is a controller for a poor-mans MUD client using ...
4
votes
1answer
613 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 ...