Threadsafe Stack

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Here is a class which I have written to create a simple Stack data structure which can be shared across multiple threads.
It's a simple LIFO which has 2 operations: one to push onto the top of the stack and the other one to pop from the top of the stack.
Please help me to find any issues with it.
package com.thread.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ThreadSafeStack<T>
private List<T> list;
private ReentrantReadWriteLock lock;
public ThreadSafeStack()
this.list = new ArrayList<T>();
lock = new ReentrantReadWriteLock();
public T pop()
try
lock.writeLock().lock();
if(!list.isEmpty())
return list.remove(list.size()-1);
finally
System.out.println("In finally");
lock.writeLock().unlock();
return null;
public boolean push(T t)
try
lock.writeLock().lock();
return list.add(t);
finally
lock.writeLock().unlock();
java stack locking
add a comment |Â
up vote
1
down vote
favorite
Here is a class which I have written to create a simple Stack data structure which can be shared across multiple threads.
It's a simple LIFO which has 2 operations: one to push onto the top of the stack and the other one to pop from the top of the stack.
Please help me to find any issues with it.
package com.thread.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ThreadSafeStack<T>
private List<T> list;
private ReentrantReadWriteLock lock;
public ThreadSafeStack()
this.list = new ArrayList<T>();
lock = new ReentrantReadWriteLock();
public T pop()
try
lock.writeLock().lock();
if(!list.isEmpty())
return list.remove(list.size()-1);
finally
System.out.println("In finally");
lock.writeLock().unlock();
return null;
public boolean push(T t)
try
lock.writeLock().lock();
return list.add(t);
finally
lock.writeLock().unlock();
java stack locking
I'm unfamiliar with thread safety in Java, but from a little research, should your data structure implementation make use of thesynchronizedkeyword? I may be wrong if that is handled byjava.util.concurrent.locks.ReentrantReadWriteLock. However, I do not see anything onReentrantReadWriteLock's documentation, it only mentions it is reentrant, not thread-safe.
â esote
Jun 28 at 3:50
What's wrong with aConcurrentLinkedQueue?
â mtj
Jun 28 at 6:07
@mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
â AbNig
Jun 28 at 17:42
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Here is a class which I have written to create a simple Stack data structure which can be shared across multiple threads.
It's a simple LIFO which has 2 operations: one to push onto the top of the stack and the other one to pop from the top of the stack.
Please help me to find any issues with it.
package com.thread.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ThreadSafeStack<T>
private List<T> list;
private ReentrantReadWriteLock lock;
public ThreadSafeStack()
this.list = new ArrayList<T>();
lock = new ReentrantReadWriteLock();
public T pop()
try
lock.writeLock().lock();
if(!list.isEmpty())
return list.remove(list.size()-1);
finally
System.out.println("In finally");
lock.writeLock().unlock();
return null;
public boolean push(T t)
try
lock.writeLock().lock();
return list.add(t);
finally
lock.writeLock().unlock();
java stack locking
Here is a class which I have written to create a simple Stack data structure which can be shared across multiple threads.
It's a simple LIFO which has 2 operations: one to push onto the top of the stack and the other one to pop from the top of the stack.
Please help me to find any issues with it.
package com.thread.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ThreadSafeStack<T>
private List<T> list;
private ReentrantReadWriteLock lock;
public ThreadSafeStack()
this.list = new ArrayList<T>();
lock = new ReentrantReadWriteLock();
public T pop()
try
lock.writeLock().lock();
if(!list.isEmpty())
return list.remove(list.size()-1);
finally
System.out.println("In finally");
lock.writeLock().unlock();
return null;
public boolean push(T t)
try
lock.writeLock().lock();
return list.add(t);
finally
lock.writeLock().unlock();
java stack locking
edited Jun 28 at 4:28
200_success
123k14143399
123k14143399
asked Jun 27 at 20:39
AbNig
113
113
I'm unfamiliar with thread safety in Java, but from a little research, should your data structure implementation make use of thesynchronizedkeyword? I may be wrong if that is handled byjava.util.concurrent.locks.ReentrantReadWriteLock. However, I do not see anything onReentrantReadWriteLock's documentation, it only mentions it is reentrant, not thread-safe.
â esote
Jun 28 at 3:50
What's wrong with aConcurrentLinkedQueue?
â mtj
Jun 28 at 6:07
@mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
â AbNig
Jun 28 at 17:42
add a comment |Â
I'm unfamiliar with thread safety in Java, but from a little research, should your data structure implementation make use of thesynchronizedkeyword? I may be wrong if that is handled byjava.util.concurrent.locks.ReentrantReadWriteLock. However, I do not see anything onReentrantReadWriteLock's documentation, it only mentions it is reentrant, not thread-safe.
â esote
Jun 28 at 3:50
What's wrong with aConcurrentLinkedQueue?
â mtj
Jun 28 at 6:07
@mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
â AbNig
Jun 28 at 17:42
I'm unfamiliar with thread safety in Java, but from a little research, should your data structure implementation make use of the
synchronized keyword? I may be wrong if that is handled by java.util.concurrent.locks.ReentrantReadWriteLock. However, I do not see anything on ReentrantReadWriteLock's documentation, it only mentions it is reentrant, not thread-safe.â esote
Jun 28 at 3:50
I'm unfamiliar with thread safety in Java, but from a little research, should your data structure implementation make use of the
synchronized keyword? I may be wrong if that is handled by java.util.concurrent.locks.ReentrantReadWriteLock. However, I do not see anything on ReentrantReadWriteLock's documentation, it only mentions it is reentrant, not thread-safe.â esote
Jun 28 at 3:50
What's wrong with a
ConcurrentLinkedQueue?â mtj
Jun 28 at 6:07
What's wrong with a
ConcurrentLinkedQueue?â mtj
Jun 28 at 6:07
@mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
â AbNig
Jun 28 at 17:42
@mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
â AbNig
Jun 28 at 17:42
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
It is unclear what you are trying to achieve but one of the below implementations should suit your requirements:
ConcurrentLinkedDeque since 1.7
BlockingDeque since 1.6
(If you're just trying to reinvent the wheel for educational purposes, I will recommend reading the source code of above mentioned java classes.)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It is unclear what you are trying to achieve but one of the below implementations should suit your requirements:
ConcurrentLinkedDeque since 1.7
BlockingDeque since 1.6
(If you're just trying to reinvent the wheel for educational purposes, I will recommend reading the source code of above mentioned java classes.)
add a comment |Â
up vote
0
down vote
It is unclear what you are trying to achieve but one of the below implementations should suit your requirements:
ConcurrentLinkedDeque since 1.7
BlockingDeque since 1.6
(If you're just trying to reinvent the wheel for educational purposes, I will recommend reading the source code of above mentioned java classes.)
add a comment |Â
up vote
0
down vote
up vote
0
down vote
It is unclear what you are trying to achieve but one of the below implementations should suit your requirements:
ConcurrentLinkedDeque since 1.7
BlockingDeque since 1.6
(If you're just trying to reinvent the wheel for educational purposes, I will recommend reading the source code of above mentioned java classes.)
It is unclear what you are trying to achieve but one of the below implementations should suit your requirements:
ConcurrentLinkedDeque since 1.7
BlockingDeque since 1.6
(If you're just trying to reinvent the wheel for educational purposes, I will recommend reading the source code of above mentioned java classes.)
answered Jun 28 at 9:58
deb_
263
263
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197378%2fthreadsafe-stack%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I'm unfamiliar with thread safety in Java, but from a little research, should your data structure implementation make use of the
synchronizedkeyword? I may be wrong if that is handled byjava.util.concurrent.locks.ReentrantReadWriteLock. However, I do not see anything onReentrantReadWriteLock's documentation, it only mentions it is reentrant, not thread-safe.â esote
Jun 28 at 3:50
What's wrong with a
ConcurrentLinkedQueue?â mtj
Jun 28 at 6:07
@mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
â AbNig
Jun 28 at 17:42