• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts  RSS feed

 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Sorry for asking this in this forum. I have some basic doubts in Java
1. class a
{
public synchronized void foo() {}
public synchronized void foo1(){}
public void foo2()
}
In this class whether can I able to call foo() & foo1() & foo2() method at the same time.
Answer: Actually my answer is that it is not possible to call method at the same time as the object is locked by the monitor.
2. class A
{
public void foo() throws Exception{}
}
class B
{
public void foo(){}
}

In both cases whether both methods are overrided or overloaded?

My Answer is it is a overrided method as it will consider only the method signature mainly the no of arguments and the type of arguement.
Regards,
M.S.Raman
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for asking this in this forum. I have some basic doubts in Java

If you know that this is not the appropriate forum for the question, why didn't you just ask it in the right one?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this has nothing at all to do with servlets, I'm moving this thread to Java in General - Intermediate.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In both cases whether both methods are overrided or overloaded?

Since the classes A and B are unrelated in your example, the two "foo" methods aren't an example of either overriding or overloading. They're just two methods with the same name. Did you mean to make one class extend the other? If B extends A, then this is fine; B's foo() overrides A's foo. But if A extends B, it'd be a compile-time error, as the methods would still override eachother (as their argument lists are the same) but the child's exception signature is less restrictive than the parent's, which is not allowed.
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for replying. Yes, I missed the extends keyword. It is class B extends class A. In this case your saying the method in B is overrided the method in A. Am I right. Do you have any idea about the first question.
Regards,
M.S.Raman



Originally posted by Ernest Friedman-Hill:

Since the classes A and B are unrelated in your example, the two "foo" methods aren't an example of either overriding or overloading. They're just two methods with the same name. Did you mean to make one class extend the other? If B extends A, then this is fine; B's foo() overrides A's foo. But if A extends B, it'd be a compile-time error, as the methods would still override eachother (as their argument lists are the same) but the child's exception signature is less restrictive than the parent's, which is not allowed.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, B.foo() overrides A.foo() in that case. And your understanding of the synchronized methods in the first question is correct.
 
Azriel Abramovich
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is aware that it might be necessary to pass from one synced method to another, thus it allows it. It allows the propagation of the calls as long as the same monitor is caught.
In your question the class monitor is caught and thus both methods will allow propagation of calls between them by the holder of the monitor.
consider the following example:
 
Azriel Abramovich
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, hit the TAB key and then pressed space.... this is to continue:
consider the following example:

Even in this case, propagation of calls from a() to b() will be possible (within the synced blox) because they have the same monitor owner.
In your example and in mine if two different monitor owners (threads) would try to 'use' the same synched area of code the synch mechanism would get to work and block one thread until the other is done.
Hope this helped.
This was made this way to avoid dead-locks.
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill & Azriel Abramovich for replying my query. Is there any way i can test this.
I tried this by running two seperate threads for printing 10000 numbers, but i am not able to differentiate it. Please help me to sort out this issue.
public class Zevel
{
Object lock1 = new Object();
Object lock2 = new Object();
int i;

public void a()
{ synchronized(lock1)
{
i++;
System.out.println("Inside a");
System.out.println(i);
}
}

public void b()
{ synchronized(lock2)
{
System.out.println("Inside b");
}
}

public static void main(String args[])
{
Zevel z= new Zevel();
for (int i= 0;i<10000;i++)
z.a();
z.b();
}
}
Regards,
M.S.Raman

Originally posted by Azriel Abramovich:
sorry, hit the TAB key and then pressed space.... this is to continue:
consider the following example:

Even in this case, propagation of calls from a() to b() will be possible (within the synced blox) because they have the same monitor owner.
In your example and in mine if two different monitor owners (threads) would try to 'use' the same synched area of code the synch mechanism would get to work and block one thread until the other is done.
Hope this helped.
This was made this way to avoid dead-locks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!