#Code to see that a class is not thread safe

25 messages · Page 1 of 1 (latest)

mental monolith
#

I have attached the question that I am answering for not thread safe class. The output changes after multiple runs
This is my code for testing if a class is not thread safe.

public class AddThread implements Runnable {
    private CardDeck seq;
    private int input;
    
    public AddThread(int input, CardDeck deck){
        this.input =input;
        this.seq=deck;
    }
    
    public void run() {
        for (int i=0;i<input;i++){
            seq.dealCard();
        }
    }
public class SimpleThreads {
    public static void main(String[] args) throws InterruptedException{
        CardDeck deck = new CardDeck();

        AddThread t1 = new AddThread(20000, deck);
        AddThread t2 = new AddThread(40000, deck);

        Thread Th1 = new Thread(t1);
        Thread Th2 = new Thread(t2);
        Th1.start();
        Th2.start();
        Th1.join();
        Th2.join();

        System.out.println(deck.sequenceNumber);
    }
}
public class UnSafeSequence {
    private int value = 0;
    public int[] sequenceNumber;
    public int dealCard(){
        return value++;
    }
    public int getResult(){
        return value;
    }
}
harsh tokenBOT
#

This post has been reserved for your question.

Hey @mental monolith! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

polar garden
#

okay?

mental monolith
#

are you going to help?

polar garden
#

Possibly. Do you need help?

mental monolith
#

yes please

#

i'm trying to show carddeck is not thread safe

#

but my code that I have doesn't fully work

polar garden
#

What do you mean?

#

Ideally, you'd explain what you mean before being asked, and before asking for help

mental monolith
# polar garden Ideally, you'd explain what you mean before being asked, and before asking for h...

so i'm trying to show that the class carddeck is not thread safe. Not thread safe basically means there a different output each time the code is being run.
To do this I made a this code

public class AddThread implements Runnable {
    private CardDeck seq;
    private int input;
    
    public AddThread(int input, CardDeck deck){
        this.input =input;
        this.seq=deck;
    }
    
    public void run() {
        for (int i=0;i<input;i++){
            seq.dealCard();
        }
    }
    
}
public class SimpleThreads {
    public static void main(String[] args) throws InterruptedException{
        CardDeck deck = new CardDeck();

        AddThread t1 = new AddThread(20000, deck);
        AddThread t2 = new AddThread(40000, deck);

        Thread Th1 = new Thread(t1);
        Thread Th2 = new Thread(t2);
        Th1.start();
        Th2.start();
        Th1.join();
        Th2.join();
        
        System.out.println(deck.sequenceNumber);
    }
}
public class UnSafeSequence {
    private int value = 0;
    public int[] sequenceNumber;
    public int dealCard(){
        return value++;
    }
    public int getResult(){
        return value;
    }
}

My code sometimes gives the same output so it's not showing the correct result

polar garden
#

Stop repeating and just explain yourself

mental monolith
#

???

polar garden
#

repeating is saying the same stuff again. That's not useful because you already said it. So don't do that

mental monolith
#

:/

polar garden
#

Instead you should use words that make it so people can get an idea of what is your situation, rather than words that make it unlikely they would

mental monolith
#

:/

polar garden
#

Most likely:
"My code sometimes gives the same output so it's not showing the correct result"
You should be able to realize that you should have never written such a sentence, or at least not without examples that show what happens

#

In other words, just be a person capable of communication

#

So:
What do you mean?

mental monolith
#

idk anymore

bold thorn
#

at least honestly

mental monolith
#

:/