rajaraman navaneethan

Ranch Hand
+ Follow
since Feb 26, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by rajaraman navaneethan

Hi Friends,
I am trying my hands on basic thread programming.

My requirement is as follows.(It is just a trial in Multi threading not a real time scenario).

I need to print the numbers from 1 to 10 and 100 to 99 in the following way

1 99 2 98 3 97 4 96.....

I have done some homework by referring 'Complete Reference' and couple other books.

I have written a class with a method which takes an integer argument and prints it.
I have synchronized the method.
I have created two threads one, printing 1 to 10, and another 99 to 90.
I start both the threads and call the method to print the number passed in.


I am not able to effectively give turns between the two threads so that, I can alternately call the method from each thread and print the number as I want.
I guess wait() and notify can be used but I am not able to get it right.

Experts, would you please help?

This my program



public class ForwardReverse {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {

X m = new X();

Forward obj1 = new Forward(m);
Thread t1 = new Thread(obj1);




Reverse obj2 = new Reverse(m);
Thread t2 = new Thread(obj2);

t1.start();
t2.start();



}

}

class Forward implements Runnable
{
X x;
int n=1;

Forward(X x)
{
this.x=x;
}

public void run()
{
while(n<=10)
{
x.print(n);
n++;
}

}
}

class Reverse implements Runnable
{
X x;
int n=100;

Reverse(X x)
{
this.x=x;
}

public void run()
{
while(n>=90)
{
x.print(n);
n--;
}

}
}


class X
{

boolean setvalue = false;

public synchronized void print(int n)
{


try
{
wait();
}

catch(InterruptedException e)
{
e.printStackTrace();
}

System.out.println(n);
setvalue = true;
notify();

}

}


Hi Swapna,
you are calling test2 method by passing button1's value.Inside the test2 method, the button reference variable points to a new Button object created inside the method.But button1 reference variable inside the main method still points null.
Similarly, you are calling test1 method by passing button2 's value.Inside the test1 method the button reference variable points to null, but still button2 variable in main method points to the created button object in the main.
Understand that in test1 and test2 the button reference variables do not change the value of the object passed rather they point to a null and newly created object respectively.
To understand it better try to print the hashcode values of the button reference variables.
hope this helps.

thanks,
Raja
Hey Ian,
I the toString() method does not seem to create a new String object.The hashCode() method returns the same value for toUpperCase() and toString() method.
So the total number of Objects created should be five.

regards,
Raja
Yes Vaibhav,
I missed out that 47.Thanks for pointing it out.

Raja
Hey Moieen Khatri,
The number of objects created here is 5.To find the number of unique objects created you can use the following in your code.

class test
{

public static void main(String args[])
{

System.out.println(makinStrings());
}

public static String makinStrings() {
String s = "Fred";
System.out.println(s+" "+s.hashCode());
s = s + "47";
System.out.println(s+" "+s.hashCode());
s = s.substring(2, 5);
System.out.println(s+" "+s.hashCode());
s = s.toUpperCase();
System.out.println(s+" "+s.hashCode());
s=s.toString();
System.out.println(s+" "+s.hashCode());
return s;
}

}

I have just a call to the hashCode() method every time I make a change to the String Object s.When I run the above code , I get the following output.

bash-3.00# java test
Fred 2198155
Fred47 2112428622
ed4 100213
ED4 68469
ED4 68469
ED4

This shows there are 5 unique String Objects created since the hashCode() method returns different values each time it is called.

Hope this Helps.

regards,
Raja
Hi Ulf Dittmer,Rahul,
Thanks for your replies.
Ulf, from your reply,

"The JVM can do both bytecode interpretation and compilation to native code. The decision which code to interpret and which code to compile is made at runtime, but generally, most of the code will be compiled."


Do you mean, that JVM can compile the .class file to native machine code?Can I compare this with microprocessor, opcode to mnemonics, similarly bytecodes(.class) to machine code?

regards,
Raja
16 years ago
Hi Friends,
I understand that JVM is written in a platform dependent language like C.Does the JVM intrepret the bytecodes to convert them into machince code understandable by the hardware?


regards,
Raja
16 years ago
Hi,
How many objects are created when I run a simple HelloWorld Program shown below.

class HelloWorld
{
public static void main(String args[])
{
System.out.println("HelloWorld");
}
}

Is java.lang.Class instance created for this class?If so, when is it created?
Can anyone please give me a good link which would help me understand java.lang.Class?

regards,
Raja
16 years ago
Hi Ilja Preuss,
Thanks for the explanation.

regards,
Raja
Hi Leandro Melo,
Thanks for the nice reply with the example.The concept is clear now.

regards,
Raja
Hi Leandro Melo,
Thanks for the quick reply.But I have a doubt.Supposing if there are two threads which are accessing the same method at the same time, and one thread passes value 10 for k, and another thread 20 for k, how will the method be thread safe?sorry if my question is too basic,I do not have real time experience in threads.

regards,
Raja
Hi,
I have a simple question about threads.

int methodA(int k)
{
k++;
return k;
}


Is the above method thread safe?or is it necessary to add synchronized keyword to make itthread safe?I have a small confusion as this method deals with method variables only and not any instance variable.Please clarify.

regards,
Raja
Hi,
Can anyone tell me, which book would help understand how the JVM works?I see two or three books like "Inside the JVM, by Bill Veneers","Programming for the Java Virtual Machine" by Engel and O'Reilly's "Java Virtual Machine".Which one is a better choice?
I have done my SCJP 1.4,SCWCD, and i have 2+ years of experience in jsp,servlets,java programming.

regards,
Raja
16 years ago
Hi Rajareddy,
When you call the run method directly a separate call stack is not created and it runs as a part of the same call stack from the method which it is called.Eventually it doesnt run concurrently with other threads.

regards,
Raja
16 years ago
Hi Friends,
Can anyone please tell me if there is a return statement from the catch block, and there is a finally block, is the value from the catch block returned first.
I am little confused as how the code flows in the following program.

public class Test1 {

static int i=0;

public static void main(String[] args) {
System.out.println(meth1());
System.out.println(i);
}

static int meth1()
{

try
{
if(i==0)
{
throw new Exception();
}
}
catch(Exception e)
{
System.out.println(i);
return ++i;
}
finally
{
System.out.println(i);
return ++i;
}

}
}
The output is 0 1 2 2.

regards,
Raja
16 years ago