Why java swing does not allow changes from an ExecutorService Thread - java

I have a problem about my java GUI management. I'm making an updater but when i'm trying to edit some JText or JProgressbar from an ExecutorService Thread, nothing..
Why is it broken ? This is my ControllerManager:
public class UpdateManager extends AppManager {
private final List<Controller> controllers;
#Getter private final ExecutorService worker;
#Getter private final UserInterface form;
private boolean isEnd;
public UpdateManager() {
this.controllers = new ArrayList<>();
this.form = new Form(this);
this.worker = Executors.newCachedThreadPool();
}
#Override
public void start(Controller... controllers) {
form.initialize();
this.controllers.addAll(Arrays.asList(controllers));
for(Controller controller: controllers)
controller.start();
}
#Override
public void end(boolean dispose) {
if(isEnd) return;
for(Controller controller: controllers)
controller.end();
if(dispose) form.dispose();
isEnd = true;
}
}
And here is an example when i'm trying to change some labels:
/**
* Created by romain on 17/05/2015.
*/
public class ReleaseController implements Controller {
private final AppManager manager;
#Getter private final LinkedBlockingDeque<URL> files;
private Future<?> future;
private final SerializedObject<SerializedReleases> serializedReleases;
private final SerializedObject<Integer> serializedRelease, serializedTimestamp;
public ReleaseController(AppManager manager) {
this.manager = manager;
this.files = new LinkedBlockingDeque<>();
this.serializedReleases = SerializedObjectImpl.create(FileUtils.path("releases", "releases.dat"), true, null);
this.serializedRelease = SerializedObjectImpl.create(FileUtils.path("swtour", "release.int"), false, 0);
this.serializedTimestamp = SerializedObjectImpl.create(FileUtils.path("swtour", "timestamp.int"), false, 0);
}
/**
* TODO: checking local files
*/
#Override
public void start() {
this.future = manager.getWorker().submit(new Runnable() {
#Override
public void run() {
int release = serializedRelease.get();
int serverRelease = serializedReleases.get().lastRelease(AppUtils.OS);
int result = serverRelease - release;
if(result == 0 || result < 0) {
manager.getForm().alreadyUpdated(); //HERE
return;
}
for(int i=release+1;i<serverRelease;i++) {
try {
files.addLast(new URL(
FileUtils.path(Main.SERVER, "releases", AppUtils.OS.toString(), i + ".zip")));
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
serializedRelease.setObject(serverRelease).write();
serializedTimestamp.setObject((int)System.currentTimeMillis()).write();
}
});
}
#Override
public void end() {
if(future != null && !future.isCancelled())
future.cancel(true);
}
}
If you saw correctly, it:
manager.getForm().alreadyUpdated();
Modifies my GUI :
public void alreadyUpdated() {
content.getFirstLine().setText("");
content.getSecondLine().setText("Your client is already up-to-date!");
content.getPlayButton().setEnabled(true);
}
but nothing.. My gui is not changed !
Thank you

There are two problems with attempting to update a Swing data structure from another thread:
Swing is not designed to be multi-thread safe. You could corrupt its data structures.
There is nothing forcing it to see changes made in another thread. It can be running with values in registers or caches that are not affected by changes to memory.
As already noted in a comment, you need to use invokeAndWait or invokeLater to make your changes in the Swing event handling thread.

Related

how to stop receiving waiting answers from livedata observe Room

I'm dealing with something that I don't understand at all.
If I delete the observer before updating my Room database and then put the observer back on, I have notifications for each update, and the recyclerview is updated as many times.
My partial code:
public class ArticlesListFragment extends Fragment {
private LiveData<List<Article>> mLDgetAllArticle;
private long mClistId;
private ArrayList<Article> mArticles;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.mLDgetAllArticle = this.articleViewModel.getAllArticle(mclistId);
this.setArticlesObserver();
...
}
private void setArticlesObserver() {
if (this.mLDgetAllArticle != null && !this.mLDgetAllArticle.hasObservers())
this.mLDgetAllArticle.observe(getViewLifecycleOwner(), this::updateArticlesList);
}
private void updateArticlesList(List<Article> articles) {
this.mArticles = new ArrayList<>(articles);
this.mArticlesRecyclerViewAdapter.setAdapterDatas(this.mArticles);
}
private void removeArticlesObserver() {
if (this.mLDgetAllArticle != null && this.mLDgetAllArticle.hasObservers())
this.mLDgetAllArticle.removeObservers(getViewLifecycleOwner());
}
private void updateArticle(Article article) {
this.articleViewModel.updateArticle(article);
}
...
}
Everything is fine so far.
But, elsewhere, I have to update all my Articles, like:
for (int i = 0; i < this.mArticles.size(); i++) {
this.mArticles.get(i).setOrd(i);
this.updateArticle(this.mArticles.get(i));
}
Also, I thought I should delete the observer before, and put it back later:
this.removeArticlesObserver();
for (int i = 0; i < this.mArticles.size(); i++) {
this.mArticles.get(i).setOrd(i);
this.updateArticle(this.mArticles.get(i));
}
this.setArticlesObserver();
but I still get after all the updates notifications. updateArticlesList is called as many times as there were updateArticles, after setArticlesObserver.
What am I missing?
Is there any way to flush all that waiting results before setting observer again?
ViewModelFactory.java:
public class ViewModelFactory implements ViewModelProvider.Factory {
private final ArticleDataRepository articleDataSource;
private final Executor executor;
private static volatile ViewModelFactory factory;
public static ViewModelFactory getInstance(Context context) {
if (factory == null) {
synchronized (ViewModelFactory.class) {
if (factory == null) {
factory = new ViewModelFactory(context);
}
}
}
return factory;
}
private ViewModelFactory(Context context) {
getDB database = getDB.getInstance(context);
this.articleDataSource = new ArticleDataRepository(database.articleDao());
this.executor = Executors.newSingleThreadExecutor();
}
#Override
#NotNull
public <T extends ViewModel> T create(Class<T> modelClass) {
if (modelClass.isAssignableFrom(ArticleViewModel.class)) {
return (T) new ArticleViewModel(articleDataSource, executor);
}
throw new IllegalArgumentException("Unknown ViewModel class");
}
}
ArticleViewModel.java:
public class ArticleViewModel extends ViewModel {
private final ArticleDataRepository articleDataSource;
private final Executor executor;
public ArticleViewModel(
ArticleDataRepository articleDataSource,
Executor executor
) {
this.articleDataSource = articleDataSource;
this.executor = executor;
}
public LiveData<List<Article>> getAllArticle(long clistId) {
return articleDataSource.getAll(clistId);
}
}
ArticleDataRepository:
public class ArticleDataRepository {
private final ArticleDao articleDao;
public ArticleDataRepository(ArticleDao articleDao) {
this.articleDao = articleDao;
}
public LiveData<List<Article>> getAll(long clistId) {
return this.articleDao.getAll(clistId);
}
}
I used this documentation
I took two steps to solve my problem:
I'm updating all the articles instead of doing a part one I didn't know I could do
#Update(onConflict = OnConflictStrategy.REPLACE)
void updateAll(ArrayList articles);
for (int i = 0; i < this.mArticles.size(); i++) {
this.mArticles.get(i).setOrd(i);
this.updateArticle(this.mArticles.get(i));
}
becomes:
for (int i = 0; i < this.mArticles.size(); i++) {
this.mArticles.get(i).setOrd(i);
}
this.updateAllArticles(this.mArticles);
I put a flag to launch the notifyDataSetChanged() only during specific events and not each time the data is updated.

Using Java stream forEach() in ScheduledExecutorService freezes

The general idea is to have a Runnable running every 10 seconds in background to check some data and if needed make changes in an object. ScheduledExecutorService is instantiated in method main() and the task is scheduled. Runnable task instantiates Crawler object and starts crawling. Most of the times it runs couple of times with success but when application is running and data changes one of crawler's method is fired but never ends. There is no loop in the code. I was trying to debug also without success. Maybe you will be able to spot where the problem lays.
Main:
public class Main {
public static void main(String[] args) {
DataStock dataStock = DataStock.getInstance();
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new EveryFiveSeconds(), 5, 5, TimeUnit.SECONDS);
// below the task which fails after couple of runs
ses.scheduleAtFixedRate(new EveryTenSeconds(), 1 , 10, TimeUnit.SECONDS);
dataStock.init();
Menu currentScreen = new UserMenu();
while(currentScreen != null) {
currentScreen = currentScreen.display();
}
}
}
EveryTenSeconds Runnable:
public class EveryTenSeconds implements Runnable {
#Override
public void run() {
Crawler crawler = new Crawler();
crawler.crawl();
}
}
Crawler:
public class Crawler {
private final DataStock dataStock;
public Crawler() {
this.dataStock = DataStock.getInstance();
}
public void crawl() {
checkOutRentables(dataStock.getCarServicesWithOwners().keySet());
checkFinancialBook(dataStock.getPaymentsBook(), dataStock.getCurrentDate());
}
private void checkOutRentables(Set<CarService> carServices) {
System.out.println("Start check...");
carServices.stream()
.flatMap(service -> service.getWarehousesSet().stream())
.filter(rentable -> !rentable.isAvailableForRent())
.forEach(RentableArea::refreshCurrentState);
System.out.println("Checking finished");
}
private void checkFinancialBook(Set<BookEntry> bookEntries, LocalDate currentDate) {
System.out.println("Start second check...");
bookEntries.stream()
.filter(bookEntry -> currentDate.isAfter(bookEntry.getPaymentDeadline()) && !bookEntry.isPaid() && !bookEntry.isNotified())
.forEach(BookEntry::notifyDebtor);
System.out.println("Finished second check..."); //this line never shows in one of runs and the task is never repeated again...
}
}
BookEntry
public class BookEntry {
private final UUID rentableId = UUID.randomUUID();
private final UUID personId;
private final UUID id;
private final BigDecimal amountDue;
private final LocalDate paymentDeadline;
private boolean paid = false;
private boolean notified = false;
public BookEntry(UUID personId, UUID id, BigDecimal amountDue, LocalDate paymentDeadline) {
this.personId = personId;
this.id = id;
this.amountDue = amountDue;
this.paymentDeadline = paymentDeadline;
}
public UUID getRentableId() {
return rentableId;
}
public UUID getPersonId() {
return personId;
}
public UUID getId() {
return id;
}
public BigDecimal getAmountDue() {
return amountDue;
}
public LocalDate getPaymentDeadline() {
return paymentDeadline;
}
public boolean isPaid() {
return paid;
}
public boolean isNotified() {
return notified;
}
public void settlePayment() {
if(!paid) {
paid = true;
}
else {
throw new IllegalStateException("This is already paid man!");
}
}
public void notifyDebtor() {
if(!notified) {
notified = true;
DataStock dataStock = DataStock.getInstance();
Person debtor = dataStock.getPeople().stream()
.filter(person -> person.getId().equals(personId))
.findFirst()
.orElseThrow();
debtor.alert(new TenantAlert(personId, rentableId, dataStock.getCurrentDate(), amountDue));
}
}
}
It seems that the answer is easy - whenever the task scheduled in ScheduledExecutorService throws an exception the task is halted and never repeated. Also the exception is not thrown visibly. The easiest way to avoid such situation is to have try-catch block in run() ,method of Runnable. Please have a look at this post: ScheduledExecutorService handling exceptions

Reduce the number of AsyncTask in Room DataBase Repository

I am trying to simplify the number of lines for my codes in the repository.
Currently there is a lot of repetition in my codes.
Many of the solutions online only involves inserting once into the table.
I need to do insert() on many tables. I want to reduce the repetition for writing the same inner AsyncTask for inserting different data into different table
This is the codes for the repository class
public class CharacterRepository {
private UserDao rUserDao;
private CharacterDao rCharacterDao;
private EquipementDao rEquipementDao;
private LiveData<List<UserDao>> rUserLD;
private LiveData<List<CharacterDao>> rCharacterLD;
private LiveData<List<EquipmentDao>> rEquipmentLD;
// Constructor that handles the database and initialise the member variables
CharacterRepository(Application application){
MyDatabase db = MyDatabase.getDatabase(application);
rUserDao = db.userDao();
rCharacterDao = db.characterDao();
rEquipementDao = db.EquipmentDao();
rUserLD = rUserDao.getAllUser();
rCharacterLD = rCharacterDao.getAllChar();
rEquipmentLD = rEquipementDao.getAllEquip();
}
// Wrapper method that returns cached entities as LiveData
public LiveData<List<UserEntity>> getAllUser(){return rUserLD;}
public LiveData<List<CharEntity>> getAllChar(){return rCharacterLD;}
public LiveData<List<EquipEntity>> getAllEquip(){return rEquipmentLD;}
/*---------------------the start of the problem-------------------*/
//Wrapper method: calling insert on non-UI Thread
public void insert(UserEntity userEntity){new insertUserAsyncTask(rUserDao).execute(userEntity);}
public void insert(CharacterEntity characterEntity){new insertCharacterAsyncTask(rCharacterDao).execute(characterEntity);}
public void insert(EquipmentEntity equipmentEntity){new insertEquipAsyncTask(rCharacterDao).execute(equipmentEntity);}
/*-------------------THIS IS THE PART WHERE I WANT TO REDUCE THE CODE REDUNDANCY THE CODES ARE DOING THE SAME THING-------------------*/
private static class insertUserAsyncTask extends AsyncTask<UserEntity, Void, Void> {
private UserDao mAsyncTaskDao;
insertUserAsyncTask(UserDao dao) {mAsyncTaskDao = dao;}
#Override
protected Void doInBackground(UserEntity... userEntities) {
mAsyncTaskDao.save(params[0]);
return null;
}
}
private static class insertCharacterAsyncTask extends AsyncTask<CharacterEntity, Void, Void> {
private CharacterDao mAsyncTaskDao;
insertCharacterAsyncTask(CharacterDao dao) {mAsyncTaskDao = dao; }
#Override
protected Void doInBackground(CharacterEntity... characterEntities) {
mAsyncTaskDao.save(params[0]);
return null;
}
}
private static class insertEquipAsyncTask extends AsyncTask<, Void, Void> {
private EquipmentDao mAsyncTaskDao;
insertEquipAsyncTask(EquipmentDao dao) {mAsyncTaskDao = dao;}
#Override
protected Void doInBackground(EquipmentEntity... equipmentEntities) {
mAsyncTaskDao.save(params[0]);
return null;
}
}
}
I still have other insert methods and I need to call delete and update as well. I do not want the codes to so repetitive
so, #notTdar came up with this solution
Have a class call ThreadPoolExecutor.
Call this class to execute all the DAO from the Android Room Database
Call cleanResource(); in onDestroy
Call shut(); in onPause
ThreadPoolExecutorHelper.java
public class ThreadPoolExecutorHelper {
private static final String TAG = ThreadPoolExecutorHelper.class.getSimpleName() + " : ";
private static final boolean LOG_DEBUG = false;
private static volatile ThreadPoolExecutorHelper INSTANCE;
private ThreadPoolExecutor mThreadPoolExecutor;
private BlockingQueue<Runnable> mBlockingQueue;
private static final int TASK_QUEUE_SIZE = 12;
//core size, keeps thread : along with running + idle
private static final int CORE_POOL_SIZE = 5;
// pool size
private static final int MAX_POOL_SIZE = 5;
// core pool size exceeds, idle thread will wait for this time before termination.
private static final long KEEP_ALIVE_TIME = 20L;
public static ThreadPoolExecutorHelper getInstance() {
if (LOG_DEBUG) Log.e(TAG, "getInstance: ");
if (INSTANCE == null) {
synchronized (ThreadPoolExecutorHelper.class) {
if (INSTANCE == null) {
INSTANCE = new ThreadPoolExecutorHelper();
}
}
}
return INSTANCE;
}
private ThreadPoolExecutorHelper() {
if (LOG_DEBUG) Log.d(TAG, "ctor: ");
initBlockingQueue();
initThreadPoolExecutor();
}
// submit Runnables
public void submitRunnable(Runnable task) {
if (LOG_DEBUG) Log.d(TAG, "submitRunnable: " + task.getClass().getSimpleName());
//in case, init again, if null.
initBlockingQueue();
initThreadPoolExecutor();
mThreadPoolExecutor.execute(task);
}
// shut the threadpool
public synchronized void shut() {
if (LOG_DEBUG) Log.d(TAG, "shut: ");
if (mThreadPoolExecutor != null) {
mThreadPoolExecutor.shutdown();
try {
mThreadPoolExecutor.awaitTermination(6000L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
if (LOG_DEBUG) Log.w(TAG, "shut: InterruptedException");
mThreadPoolExecutor.shutdownNow();
}
} else {
Log.e(TAG, "shut: mThreadPoolExecutor instance NULL");
}
}
//clean up
public void cleanResources() {
if (LOG_DEBUG) Log.e(TAG, "cleanResources: ");
if (INSTANCE != null) {
if (mThreadPoolExecutor != null) {
mThreadPoolExecutor = null;
}
if (mBlockingQueue != null) {
mBlockingQueue = null;
}
nullifyHelper();
}
}
private static void nullifyHelper() {
if (INSTANCE != null) {
INSTANCE = null;
}
}
private void initBlockingQueue() {
if (mBlockingQueue == null) {
mBlockingQueue = new LinkedBlockingQueue<>(TASK_QUEUE_SIZE);
}
}
private void initThreadPoolExecutor() {
if (mThreadPoolExecutor == null) {
mThreadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
KEEP_ALIVE_TIME, TimeUnit.SECONDS, mBlockingQueue);
}
}
}
Add this codes in onCreate (activity) or onViewCreated(Fragment)
This will initialise the ThreadPoolExecutorHelper by calling getInstance()
private void initExecutorHelper() {
if (LOG_DEBUG) Log.d(TAG, "initExecutorHelper: ");
if (mExecutorHelper == null) {
mExecutorHelper = ThreadPoolExecutorHelper.getInstance();
}
}
This is the insert(); method to start a Thread
You can change this to do insert, query, delete task from the DAO in Room Database
public void insert() {
if (LOG_DEBUG) Log.d(TAG, "requestQREntityList: whatKind= " + whatKind);
mExecutorHelper.submitRunnable(() -> {
if (!Thread.interrupted()) {
//request a list or inset something, write your logic.
} else {
if (LOG_DEBUG) Log.e(TAG, "run: Thread is interrupted");
}
});
}

Is there a "clean" way to automatically un-register a listener in Java?

I believe I've seen variants of this question, but no "definitive answer". In the code below, I understand that SomeEventManager holds a reference to someImplClassTwo.myEventListenerA and someImplClassTwo.myEventListenerB, and that this does not allow for someImplClassTwo to be garbage collected, and this results in the output generated the second time someEventManager.notifyListeners() is invoked.
But, I'd really like for users of SomeImplClass not to have to know that there are listeners involved in the implementation, and that these listeners need to be manually un-registered (i.e., SomeImplClass.releaseListeners()) before releasing the SomeImplClass object.
Is there a clean/accepted way of doing this?
p.s. I've already played with finalize(), just for fun, and confirmed that GC is not even attempted in this case, for either instance of SomeImplClass. So, that seems to be a non-starter as a potential solution.
Test Driver
public class TestDriver {
public static void main(String[] args) {
SomeEventManager someEventManager = SomeEventManager.getInstance();
SomeImplClass someImplClassOne = new SomeImplClass("One");
SomeImplClass someImplClassTwo = new SomeImplClass("Two");
someEventManager.notifyListeners();
someImplClassOne.releaseListeners();
someImplClassOne = null;
someImplClassTwo = null;
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
}
someEventManager.notifyListeners();
}
}
Event Interface
public interface SomeEventListener {
public void handleSomeEvent();
}
Event Manager
import java.util.ArrayList;
import java.util.List;
public class SomeEventManager {
private static SomeEventManager eventManager = null;
private List<SomeEventListener> listeners = null;
private SomeEventManager() {
listeners = new ArrayList<SomeEventListener>();
}
public static SomeEventManager getInstance() {
if (eventManager == null) {
eventManager = new SomeEventManager();
}
return eventManager;
}
public void addListener(SomeEventListener listener) {
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
public void removeListener(SomeEventListener listener) {
listeners.remove(listener);
}
public void notifyListeners() {
for(SomeEventListener listener : listeners) {
listener.handleSomeEvent();
}
}
}
Event Listener Implementation
public class SomeImplClass {
private InnerEventListener myEventListenerA = null;
private InnerEventListener myEventListenerB = null;
private String id = null;
public SomeImplClass(String id) {
this.id = id;
myEventListenerA = new InnerEventListener(id + "_A");
myEventListenerB = new InnerEventListener(id + "_B");
}
public void releaseListeners() {
myEventListenerA.unregisterListener();
myEventListenerB.unregisterListener();
}
private class InnerEventListener implements SomeEventListener {
private SomeEventManager someEventManager = null;
private String id = null;
public InnerEventListener(String id) {
someEventManager = SomeEventManager.getInstance();
this.id = id;
registerListener();
}
public void registerListener() {
someEventManager.addListener(this);
}
public void unregisterListener() {
someEventManager.removeListener(this);
}
public void handleSomeEvent() {
System.out.println("InnerEventListener->" + id);
}
}
}
The solution we use is to have the listener automatically unregister itself if it gets called and the thing it's updating has been collected.
It looks a bit like this:
private static class InnerEventListener implements SomeEventListener {
private final WeakReference<ThingToUpdate> thingRef;
public InnerEventListener(ThingToUpdate thing) {
thingRef = new WeakReference<>(thing);
}
#Override
public void handleSomeEvent(SomeEvent event) {
ThingToUpdate thing = thingRef.get();
if (thing != null) {
thing.updateSomehow();
} else {
((SomeEventedThing) event.getSource())
.removeSomeEventListener(this);
}
}
}
//...
SomeEventedThing eventedThing;
ThingToUpdate thingToUpdate;
//...
eventedThing.addListener(new InnerEventListener(thingToUpdate));
I wouldn't say it's a perfect solution because the listener sticks around until it gets an event, and it's still somewhat dependent on garbage collection. We've been trying to replace it with explicit removal where possible, usually on addNotify/removeNotify on GUI components.

Concurrent tests: test case scenario automatization

Task definition: I need to test custom concurrent collection or some container which manipulates with collections in concurrent environment. More precisely - I've read-API and write-API. I should test if there is any scenarios where I can get inconsistent data.
Problem: All concurrent test frameworks (like MultiThreadedTC, look at MultiThreadedTc section of my question) just provides you an ability to control the asynchronous code execution sequence. I mean you should suppose a critical scenarios by your own.
Broad question: Is there frameworks that can take annotations like #SharedResource, #readAPI, #writeAPI and check if your data will always be consistent? Is that impossible or I just leak a startup idea?
Annotation: If there is no such framework, but you find the idea attractive, you are welcome to contact me or propose your ideas.
Narrow question: I'm new in concurrency. So can you suggest which scenarios should I test in the code below? (look at PeerContainer class)
PeerContainer:
public class PeersContainer {
public class DaemonThreadFactory implements ThreadFactory {
private int counter = 1;
private final String prefix = "Daemon";
#Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, prefix + "-" + counter);
thread.setDaemon(true);
counter++;
return thread;
}
}
private static class CacheCleaner implements Runnable {
private final Cache<Long, BlockingDeque<Peer>> cache;
public CacheCleaner(Cache<Long, BlockingDeque<Peer>> cache) {
this.cache = cache;
Thread.currentThread().setDaemon(true);
}
#Override
public void run() {
cache.cleanUp();
}
}
private final static int MAX_CACHE_SIZE = 100;
private final static int STRIPES_AMOUNT = 10;
private final static int PEER_ACCESS_TIMEOUT_MIN = 30;
private final static int CACHE_CLEAN_FREQUENCY_MIN = 1;
private final static PeersContainer INSTANCE;
private final Cache<Long, BlockingDeque<Peer>> peers = CacheBuilder.newBuilder()
.maximumSize(MAX_CACHE_SIZE)
.expireAfterWrite(PEER_ACCESS_TIMEOUT_MIN, TimeUnit.MINUTES)
.removalListener(new RemovalListener<Long, BlockingDeque<Peer>>() {
public void onRemoval(RemovalNotification<Long, BlockingDeque<Peer>> removal) {
if (removal.getCause() == RemovalCause.EXPIRED) {
for (Peer peer : removal.getValue()) {
peer.sendLogoutResponse(peer);
}
}
}
})
.build();
private final Striped<Lock> stripes = Striped.lock(STRIPES_AMOUNT);
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, new DaemonThreadFactory());
private PeersContainer() {
scheduledExecutorService.schedule(new CacheCleaner(peers), CACHE_CLEAN_FREQUENCY_MIN, TimeUnit.MINUTES);
}
static {
INSTANCE = new PeersContainer();
}
public static PeersContainer getInstance() {
return INSTANCE;
}
private final Cache<Long, UserAuthorities> authToRestore = CacheBuilder.newBuilder()
.maximumSize(MAX_CACHE_SIZE)
.expireAfterWrite(PEER_ACCESS_TIMEOUT_MIN, TimeUnit.MINUTES)
.build();
public Collection<Peer> getPeers(long sessionId) {
return Collections.unmodifiableCollection(peers.getIfPresent(sessionId));
}
public Collection<Peer> getAllPeers() {
BlockingDeque<Peer> result = new LinkedBlockingDeque<Peer>();
for (BlockingDeque<Peer> deque : peers.asMap().values()) {
result.addAll(deque);
}
return Collections.unmodifiableCollection(result);
}
public boolean addPeer(Peer peer) {
long key = peer.getSessionId();
Lock lock = stripes.get(key);
lock.lock();
try {
BlockingDeque<Peer> userPeers = peers.getIfPresent(key);
if (userPeers == null) {
userPeers = new LinkedBlockingDeque<Peer>();
peers.put(key, userPeers);
}
UserAuthorities authorities = restoreSession(key);
if (authorities != null) {
peer.setAuthorities(authorities);
}
return userPeers.offer(peer);
} finally {
lock.unlock();
}
}
public void removePeer(Peer peer) {
long sessionId = peer.getSessionId();
Lock lock = stripes.get(sessionId);
lock.lock();
try {
BlockingDeque<Peer> userPeers = peers.getIfPresent(sessionId);
if (userPeers != null && !userPeers.isEmpty()) {
UserAuthorities authorities = userPeers.getFirst().getAuthorities();
authToRestore.put(sessionId, authorities);
userPeers.remove(peer);
}
} finally {
lock.unlock();
}
}
void removePeers(long sessionId) {
Lock lock = stripes.get(sessionId);
lock.lock();
try {
peers.invalidate(sessionId);
authToRestore.invalidate(sessionId);
} finally {
lock.unlock();
}
}
private UserAuthorities restoreSession(long sessionId) {
BlockingDeque<Peer> activePeers = peers.getIfPresent(sessionId);
return (activePeers != null && !activePeers.isEmpty()) ? activePeers.getFirst().getAuthorities() : authToRestore.getIfPresent(sessionId);
}
public void resetAccessedTimeout(long sessionId) {
Lock lock = stripes.get(sessionId);
lock.lock();
try {
BlockingDeque<Peer> deque = peers.getIfPresent(sessionId);
peers.invalidate(sessionId);
peers.put(sessionId, deque);
} finally {
lock.unlock();
}
}
}
MultiThreadedTC test case sample: [optional section of question]
public class ProducerConsumerTest extends MultithreadedTestCase {
private LinkedTransferQueue<String> queue;
#Override
public void initialize() {
super.initialize();
queue = new LinkedTransferQueue<String>();
}
public void thread1() throws InterruptedException {
String ret = queue.take();
}
public void thread2() throws InterruptedException {
waitForTick(1);
String ret = queue.take();
}
public void thread3() {
waitForTick(1);
waitForTick(2);
queue.put("Event 1");
queue.put("Event 2");
}
#Override
public void finish() {
super.finish();
assertEquals(true, queue.size() == 0);
}
}
Sounds like a job for static analysis, not testing, unless you have time to run multiple trillions of test cases. You pretty much can't test multithreaded behaviour - test behaviour in a single thread, then prove the abscence of threading bugs.
Try:
http://www.contemplateltd.com/threadsafe
http://checkthread.org/

Categories

Resources