Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default output not as expected.What's wrong?

    I am very new to java and trying to pass the data between threads.
    I dont get the output as expected. I would really appreciate if any one can point out what's wrong with the below program.

    Description:
    Starting the four threads for class main,tr1,tr2,bistry.
    tr1 run method calls setx of bistry to load the private variable a of bistry.
    similarly tr2 run method calls sety of bistry to load the private variable b of bistry. Setx of bistry calls setxy to add a and b and display c and lastly a,b.

    Now i Expected:
    a0b0
    c0
    a1b1
    c2
    a2b2
    c4
    a3b3
    c6....

    But the output is
    c1
    a1b0
    c2
    a2b0
    c3
    a3b0
    c4
    a4b0
    c5
    a5b0
    c6
    a6b0
    c7
    a7b0
    c8
    a8b0
    c9
    a9b0
    c10
    a10b0

    b is not updated.

    -----program------------
    public class tr1 implements Runnable
    {
    public int x;
    public int y;

    public tr1()
    {
    x = 2;
    }
    public void run()
    {

    int i = 0;
    bistry N = new bistry();
    while(true){

    if(i < 10)
    { i = i + 1;

    N.setx(i);

    }
    }
    }



    public class tr2 implements Runnable
    {
    public int W;
    public int V;
    public int i;

    public tr2()
    {
    V = 4;
    }
    public void run()
    {

    bistry M = new bistry();


    while(true){
    if(i < 5)
    {
    i = i + 1;

    M.sety(i);

    }
    }
    }



    }





    public class bistry implements Runnable
    {
    public int a;
    public int b;
    public int i ;
    public int N = 0;
    int c;
    public bistry()
    {
    i = 0;
    }
    public void run()
    {

    while(true)
    {



    }
    }


    synchronized public void setx(int x)
    {
    a = x;
    setxy(a,b);
    System.out.println("a"+a+"b"+b);


    }
    synchronized public void sety(int x)
    {
    b = x;




    }
    public void setxy(int x , int y)
    {
    c = x + y;
    System.out.println("c"+c);
    }

    }



    public class main
    {


    public static void main( String args[] ) throws Exception
    {
    int j = 0;
    tr1 R = new tr1();
    tr2 S = new tr2();
    bistry T = new bistry();
    new Thread(R).start();
    new Thread(S).start();
    new Thread(T).start();


    while(true)
    {

    }
    }
    }

  2. #2
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    First of all, you should define variables with names that help you and other to understand your code... a,b,c,x,y,z... these variable names dont help at all.

    1. a value. When you enter in tr1 while loop, you increment to 1 variable 'i' or 'a' (see my first reply lines) before you call SetX() method. You could do:

    do {
    N.SetX(i);
    }while{
    if(i < 9){
    i = i + 1;
    }
    }

    2. 'c' output is wrong because 'a' is wrong (c = a + b)

    3. 'b' value is 0 because you dont give any b value. Look at this:

    synchronized public void SetX(int x){
    a = x;
    setxy(a,b);
    System.out.println("a"+a+"b"+b);
    }

    This code sets input x to a. Right.
    But then you call SetXy(a,b) where you dont tell 'b' value and it takes initial value (b=0).

    4. Also, you should call method SetXy() AFTER printing a,b in order to get your output values expected order.

    Try it and tell us ^^.
    Last edited by SmilingKey; 12-03-2010 at 08:04 PM.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Wrong Output (Java Program)
    By poupas in forum New To Java
    Replies: 12
    Last Post: 11-28-2010, 04:28 PM
  3. Whats Wrong I m nt Getting output -please please help
    By divakantdinesh in forum New To Java
    Replies: 3
    Last Post: 10-16-2010, 09:21 PM
  4. output not what expected!
    By aza101 in forum Java Applets
    Replies: 0
    Last Post: 07-10-2009, 06:17 AM
  5. Replies: 11
    Last Post: 05-24-2008, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •