• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

multiple start calls on the same object. No Complaints!!

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A start call can be made only once on an object. here in this case, its otherwise. I made a call to main through the code explicitly which instantiated the class and ran the subsequent codelines which includes a call to start on that new object. however. when the JVM calls the main method, it does not generate an illegalMonitorStateException. Is this not some kind of a trick that is fooling JVM ?In all there are 2 calls to start but the run method is executed THRICE. My interpretation is probably wrong.seeking your opinion. Also, i have one silly question :

here it goes :

what is the difference between :

String s = "java" and String s = new String("java") and also the likes
Integer i = 1 and Integer i = new Integer(1)

Now back to the thread question( I am really sorry to be so heavily loaded with questions)

class Try extends Thread
{
static int i=0;
static int j =0;
static Try a;
Try()
{
go();
}


static
{
a=new Try();
a.start();

System.out.println("First this");
main("Andy");
}

public void run()
{
System.out.println("Into Run"+(++i));
for(int k=0;k<5;k++)
System.out.println(k+2);
}
public void go()
{
System.out.println("Hello");

}

public static void main(String... rgs)
{
Try a1= new Try();
a1.start();

System.out.println("this is main"+(++j));
System.out.println(a.toString());
}


}

Here's the output :
Hello
First this
Hello
this is main1
Thread[Thread-0,5,main]
Into Run1
2
3
4
5
6
Into Run2
2
3
4
5
6
Hello
Into Run3
2
3
4
5
6
this is main2
Thread[Thread-0,5,]

Thanks a lot for any help...
 
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhishek! Welcome to javaranch.

The code is behaving normally. The run method is indeed called thrice. Once in the static initializer block using start method-



Then again two lines later when you yourself call main-



The code in the main method will call start.



Then at last when the JVM calls main method. Now there is no IllegalStateException as both the calls to main method i.e. one by you and one by JVM, will have a separate copy of a1. a1 is local to main method. So both times main is called, a different copy of a1 will be created...
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Thanks a ton for replyin Ankit. Now, When i make the reference a1 inside main "static" by declaring it outside main, the entire class shares the same copy of that reference.However, the output is the same. run still runs thrice.
by the way Ankit, i had another question in my previous post. Could you please answer that one as well.

Thanks a lot once again.

Wish you too a very happy new year.
 
Ankit Garg
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No if you you call start on a static field, then you will get an IllegalThreadStateException



The difference between String s = "java" and String s = new String("java") is the String s = "java" will assign a string from the string pool that JVM maintains if there is already a string containing the text "java" in the pool. But String s = new String("java") will always create a new string and assign it to s.

Same is the case with Integer i = 1 and Integer i = new Integer(1). The first one assigns a value from the integer pool if there is already a value but the second one always creates a new object...
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No No Ankit, what i have done is

class Try
{
//other code
static Try a1;
....
...
...
public static void main()
{
a1 = new Try();
a1.start();
}

So run executes thrice . No exception... It runs fine...

thanks for the other answer Ankit...thanks a lot
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..And Ankit, i am sorry but i dont get wat you mean by, "if there's already a string with that name in the JVM pool "....where do those values in the pool come from ?
I am sorry to be so pesky Ankit
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhishek,

You should Use Code Tags.
 
Ankit Garg
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew that you would be doing this. That is why I provided a full code. In your code the reference used to call start is the same in both invocations. Although you are calling start on a1 both the times, but that is only a reference. The actual object is different both the times

a1 = new Try();

a1 is just the reference.

a1 = new Try();

This is the actual object which will be created each time the main method is called...
 
Ankit Garg
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you create a String like this

String s1 = "abhishek";

Now the JVM will create a string "abhishek" in the string pool. This string object will be assigned to s1. Now if in the next statement you write

String s1 = "abhishek";
String s2 = "abhishek";

Then the JVM will see that the value abhishek is already in the pool. So instead of creating a new string object, the value from the pool will be assigned to s2...
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Oh Oh! I am clear now..i am totally clear...never realized that...cant stop thanking you buddy.

Regards
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Oh Oh! I am clear now..i am totally clear...never realized that...cant stop thanking you buddy.

Regards
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit,

String s = "java"; and String s1 = new String("java");
behave similarly. Are they just two ways of assignment ?

Arent both of them placed on the heap ?
 
Ankit Garg
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are placed on the heap, but with the new syntax, the value is not taken from the pool. Look at this

String s1 = "abhishek";
String s2 = "abhishek";

Here there will be only one object in the heap representing the string abhishek. Both s1 and s2 will point to that object on the heap. That part of the heap is also called as string pool. But in the following case

String s1 = new String("abhishek");
String s2 = new String("abhishek");

Here both s1 and s2 will point to a separate string object in the heap as we have used new which will always create a new object...
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Ankit.

No wonder you scored 98%. By the way, I havent heard about people who have scored 100%. is there anyone on Javaranch who has scored 100 ?
 
Ankit Garg
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have heard about two. One of them is a moderator and one result I saw about 3 months ago. You can search in the certification results forum
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhishek see this tutorial String reference constant pool
 
Abhishek Bhat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Punit and Ankit.
 
I love a good mentalist. And so does this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic