1

I implemented a simple thread program in which one thread pushes element into a stack and the other pops element from the stack. I'm getting an unexpected output in which thread1 pushes an element twice.

import java.io.*;
import java.lang.*;
import java.util.*;

public class Threading_Sample implements Runnable
{
    private Thread T1,T2;
    String ThreadName="";

    Stack<Integer> Stack1=new Stack<Integer>();


    public void Push_Element_to_Stack(int element)
    {
        Stack1.push(element);
    }

    public void Pop_Element_from_Stack()
    {
        Stack1.pop();
    }

    public void run()
    {
      try
       {
        //while(Thread.currentThread().isAlive())
        for(int i=0;i<10;i++)
        {
            if(T1.getName().equals("THREAD1"))
                {
                    System.out.println("Current Thread: "+T1.getName());
                    System.out.println("DOING TASK 1...");

                    Push_Element_to_Stack(i);
                    Thread.sleep(100);
                    System.out.println("Stack1 of Thread1: "+Stack1);
                }
                if(T2.getName().equals("THREAD2"))
                {
                    System.out.println("Current Thread: "+T2.getName());
                    System.out.println("DOING TASK 2...");

                System.out.println("Stack of Thread2 before: "+Stack1);
                    Pop_Element_from_Stack();
                    Thread.sleep(100);
                    System.out.println("Stack of Thread2 after: "+Stack1);
                }
          }

       }
       catch(Exception e)
       {
           System.out.println("Thread interrupted...");
           e.printStackTrace();
       }
    }

    public void start()
    {
        if(T1==null)
        {
            T1=new Thread(this,"THREAD1");
            T1.start();
            try
            {
                T1.join();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        if(T2==null)
        {
            T2=new Thread(this,"THREAD2");
            T2.start();
            try
            {
                T2.join();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void main (String[] args)
    {
        Threading_Sample TASK1=new Threading_Sample();

        TASK1.start();
    }
}

OUTPUT

--------------------Configuration: <Default>--------------------
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0]
Thread interrupted...
java.lang.NullPointerException
    at Threading_Sample.run(Threading_Sample.java:39)
    at java.lang.Thread.run(Thread.java:722)
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 0]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 0]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 1]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 1]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 2]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 2]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 3]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 3]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 4]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 4]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 5]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 5]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 6]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 6]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 7]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 7]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 8]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 8]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 9]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 9]
Stack of Thread2 after: [0]

Process completed.

Why is the element 0 pushed twice into the stack ?

Regards, Rajesh.

1 Answer 1

3

You want to check current threads's name

 Thread.currentThread().getName()

not T1's name in run()

it would always be true regardless of which Thread is executing it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.