Results 1 to 2 of 2
- 12-03-2010, 03:37 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 20
- Rep Power
- 0
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)
{
}
}
}
- 12-03-2010, 09:01 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
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 09:04 PM.
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 02:37 AM -
Wrong Output (Java Program)
By poupas in forum New To JavaReplies: 12Last Post: 11-28-2010, 05:28 PM -
Whats Wrong I m nt Getting output -please please help
By divakantdinesh in forum New To JavaReplies: 3Last Post: 10-16-2010, 10:21 PM -
output not what expected!
By aza101 in forum Java AppletsReplies: 0Last Post: 07-10-2009, 07:17 AM -
[SOLVED] Expected errors, I can't see what's wrong.
By Azzia in forum New To JavaReplies: 11Last Post: 05-24-2008, 05:26 AM
Bookmarks