kamal shah

Greenhorn
+ Follow
since Feb 26, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
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 kamal shah

I am using “java.net.HttpURLConnection” method to establish the connection. When it connects with URL and receiving no response from host, the program will hang for indefinite time.

The JDK version used is 1.4. I am seeking for the solution to define timeout at the time of connection creation. So that if scheduler will not receive any response, it will come out form the program at defined time.

Code Used for connection:

**********************************************************************************************************
URL url = new URL(stURL);
HttpURLConnection conHttp = (HttpURLConnection)url.openConnection();


conHttp.setDoOutput(true);
conHttp.setRequestMethod("POST");
conHttp.setRequestProperty("Content-Type","text/xml");
conHttp.setRequestProperty("Authorization","Basic " + encoding);


conHttp.setUseCaches(true);
OutputStream outputStream = conHttp.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream);
String xmlContent = strToPost;

writer.println(xmlContent);
writer.flush();
outputStream.close();


responseCode=conHttp.getResponseCode(); // when no response, program hangs at this statement.
if(responseCode>=203 && responseCode<=505)
{
//do something
}

InputStream inputStream = conHttp.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

String xmlResult = "";
while ((xmlResult = br.readLine()) != null)
{
resp+=xmlResult+"\n";
}
***************************************************************************************************************





I have tried the below solution but it didn’t work:
I have tried to set the below properties for the timeout, it is working fine with HTTP Protocol (http url’s) but doesn’t work with HTTPS(https url’s).

1.sun.net.client.defaultConnectTimeout (default: -1)
2.sun.net.client.defaultReadTimeout (default: -1)

Please revert if you have faced the same problem or you have any idea/ solution for this issue.

Thanks n regards,
Kamal
Congratulations...Sapana
Yes,Guys..I was wrong in saying it as 'class lock',My mean was that it will behave exactly the same way as class lock do.

I do not know the exact word for this,so I used the static lock.
Congratulations!!! Sagar..
14 years ago
Ramesh,

Yes you are right.By static lock my mean was Class lock.

Sumit,

Yes,If we will use same t1 for both threads,lock on 'this' will work absolutely fine.
Here System is a class and out is the static object variable of this class having type of PrintStream.
I mean System.out is static lock.So there is only one lock for both the object t1 and t2.



But when you use 'this',it means lock of current object.t1 and t2 both will have there own lock for 'this'.

That is why in case 1 i.e. System.out,output is all one's than all two's but in case 2 i.e. 'this' lock output is unpredictable.

Now, if I keep lock on 'this' only and start both threads with 't1' as runnable object, I will get nice output of either all One's first or all Two's first.



I am confused with this statement.Please explain this.
Yes,you are right if the notify get called before the wait,it would get stuck.
Hi,

See compiler dose not have beautiful mind like us to think or assume.it works based on some rules.

Final variable is not suppose to change through out your program,to ensure this compiler want you to assign a fixed value to this variable at the time of declaration.

#Case 1:
final int a=10;

#Case 2:
final int a;
a=10;

if Case 2 would have allowed by the compiler than final variable can be reassigned any time you want in your program.Compiler has no way to ensure the functionality of final variable other than compelling you to assign it a value at the time of declaration.

Value assigned in both the case is same i.e. 10 but the way you do really matters.
While working on your problem,I end up with the below code.
Just want to share with you might it will help you to clarify wait,notify and lock of threads.

Please run the below and see how it works.
<blockquote>code:
<pre name="code" class="core">

public class WaitTest implements Runnable {

public static Object a =new Object();

public static void main(String [] args) {

System.out.print("Our Program Starts Now");
/*****Same lock will needed to notify************/
a=(Object)args;
/*****Same lock will needed to notify************/

/********To wakeup the Main Thread************/
WaitTest tObj=new WaitTest();
Thread t=new Thread(tObj);
t.start();
/********To wakeup the Main Thread************/

synchronized(args){
System.out.print("Going to get wait on args lock "+args);
try {
args.wait();
} catch(InterruptedException e){System.out.println("Exception in getting wait on agrs lock"+e);}

}

System.out.print("Wait has been over!! Thanks");


}

public void run(){
release();
}

public void release(){

//Insert your code here once done,it will notify the main thread.

synchronized(a){
System.out.println("Going to help main thread"+a);
try{

System.out.println("Calling notify to release main thread");
a.notify();
}catch(IllegalMonitorStateException e){System.out.println("Exception in calling notify "+e);}

}
System.out.println("Call of notify Done");
}
}



</pre>
</blockquote>
<blockquote>code:
<pre name="code" class="core"> import java.io.*;
public class SerializeDog {
public static void main(String[] args){
Collar c = new Collar(3);
Dog d = new Dog(c,7);
System.out.println("Before serialization the collar size is :"+ (d.getCollar()).getCollarSize());
System.out.println("Collor referece is ::"+d.getCollar());
System.out.println("Dog referece is ::"+d);
try {
FileOutputStream fos = new FileOutputStream("myfile.txt");
ObjectOutputStream os = new ObjectOutputStream (fos);
os.writeObject(d);

os.close();
}catch(Exception e) {
e.printStackTrace();
}

try {
FileInputStream fis = new FileInputStream("myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
}catch(Exception e) {
e.printStackTrace();
}

System.out.println("After deserialization the collar size is :"+ (d.getCollar()).getCollarSize());
System.out.println("Collor referece is ::"+d.getCollar());
System.out.println("Dog referece is ::"+d);
}
}

class Dog implements Serializable {
private Collar theCollar;
private int dogSize;

public Dog(Collar collar,int size) {
theCollar = collar;
dogSize = size;
}
public Collar getCollar() {
return theCollar;
}
}

class Collar implements Serializable {
private int collarSize;

public Collar(int size) {
collarSize = size;
}

public int getCollarSize(){
return collarSize;
}
}
</pre>
</blockquote>

Shashi Run the above code,And watch the dog and collar references values.It will surely clear your doubt.
When you want to regulate/govern the instantiation of your class,you make the constructor private or protected.

It can be used to achieve singleton or factory pattern.lets have a look on two cases:

1.Constructor is Private::

In this case it will restrict the inheritance and instantiation of this class by any outside class.But Still you can provide the public static method
to get the instance(any no of instance) of this class.To make this singleton you need to write your own piece of code.

Example of Singleton with private constructor

public class ClassName{
private static ClassName instance = null;

private ClassName(){
//To defeat instantiation and inheritance.
};

public static ClassName getInstance() {

/****To make the class follow singleTon*********/
if(instance == null) {
instance = new ClassName();
}
return instance;

}


2.Constructor is Protected::

In this case it will restrict the instantiation of this class for out side classes.But at the same time protected constructor follow the rule
of protected access modifier i.e. any class in same package can instantiate this class as they can access the protected constructor as per rule.

public class ClassName{
private static ClassName instance = null;

protected ClassName(){
//To defeat instantiation only.Still it can be inherited.

};

public static ClassName getInstance() {
instance = new ClassName();
return instance;
}
}
notify will wake up one thread and notifyAll will wake up all threads which are waiting on that object.

if you have 3 threads which are waiting on same object.if you call notify one among the 3 will wake up and move to ready queue...but 2 still be in wait mode.

But if you call notifyAll all 3 will wake up and move to ready queue.
No.......Just copy this program and run this on your machine you will find something very interesting!!!
Variable can not be override in Java.Here by creating new nat in subclass you are just shadowing the variable of super class.
Now if you still want to print nat of subclass i.e. HarMol,use this.nat instead of hs.nat.But in this way also you are calling it by subclass reference.
Hi All!!

while working on one of topic posted in the forum..i end up with very tricky question...here it is..

what will be the output of the below program!!!