Threadsafe Stack

The name of the pictureThe name of the pictureThe name of the pictureClash 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();










share|improve this question





















  • 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










  • @mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
    – AbNig
    Jun 28 at 17:42
















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();










share|improve this question





















  • 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










  • @mtj - I am trying to learn concurrency, thread safety and locking. So trying to use bare bones JDK.
    – AbNig
    Jun 28 at 17:42












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();










share|improve this question













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();












share|improve this question












share|improve this question




share|improve this question








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 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










  • @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










  • 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















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










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.)






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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.)






    share|improve this answer

























      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.)






      share|improve this answer























        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.)






        share|improve this answer













        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.)







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 28 at 9:58









        deb_

        263




        263






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods