lundi 20 avril 2015

counter value is not being implemented when threads run

Vote count: 0

I have created the following program which runs two threads with the same thread instance.I have initialised counter and when each of the thread runs, the counter value should be implemented and at the at end it should output in console.

public class MTThread {

static int count = 0;

public static void main(String arg[]){

    System.out.println("Main started");
    MTThreadInner in1= new MTThreadInner(1,count);
    MTThreadInner in2= new MTThreadInner(2,count);

    in1.start();

    in2.start();
    while (true) {
        try {
             in1.join();
            in2.join();
            break;
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    System.out.println("Main finished, count = :"+ count);

}}

 class MTThreadInner extends Thread {
         int num; int counter;
         MTThreadInner(int i,int z) {
            counter=z;
            num = i;

         }

        public void run(){
                // TODO Auto-generated constructor stub
            yield();
            System.out.println("This ran Thread "+ num);
            counter=counter+1;

         }


    }

asked 26 secs ago



counter value is not being implemented when threads run

Aucun commentaire:

Enregistrer un commentaire