I neeed answers for these

Multithreading may be correctly accomplished in Java in which of the following ways?

a)Extend the Thread class, provide an implementation for the start method and create an instance of this new subclass b)Create a class which implements the Threadable interface, and pass an instance of this class to a Thread Class constructor c)All of the above d)None of the above

Locking is the main mechanism for restricting simultaneous thread access to specific java code. Which of the following statement(s) are true regarding locks / monitors? a)One lock / monitor is associated with each Class b)One lock / monitor is associated with each Synchronized block c)One lock / monitor is associated with each Thread d)One lock / monitor is associated with each Object e)All of the above With respect to User and Daemon threads: a)Daemon threads can not be destroyed b)Running User threads prevent a Java VM from exiting c)Running Daemon threads prevent a Java VM from exiting d)Daemon threads can not be grouped together e)The Java VM will exit when no non-daemon threads are running

Hi This message was posted here long back.Today I had gone through this. 1.With respect to User and Daemon threads: a)Daemon threads can not be destroyed b)Running User threads prevent a Java VM from exiting c)Running Daemon threads prevent a Java VM from exiting d)Daemon threads can not be grouped together e)The Java VM will exit when no non-daemon threads are running . Answer given by paul is c.But I think the answer for this is b. 2.Locking is the main mechanism for restricting simultaneous thread access to specific java code. Which of the following statement(s) are true regarding locks / monitors? a)One lock / monitor is associated with each Class b)One lock / monitor is associated with each Synchronized block c)One lock / monitor is associated with each Thread d)One lock / monitor is associated with each Object e)All of the above For this paul has given the answers as a, b and d. But I had read somewhere that the lock is only associated with synchronized block. Can anyone throw somelight on this to help me with explanation. Thanks and regards Sunita

2. I think the that lock is only associated to object and class.In synchronize block also u have to specify an object whose lock will be obtained before executing the block. So answer should be only object and class.

I agree with Puneet. Locks/Moniters are associated with only Objects and Classes. If a class has many Synchronized methods, it doesnt mean that is has many locks. There is only one lock associated with the object. so when one synchronized method is executing, another synchronized method cannot be executed.

1) D 2) It depends on what "lock association means". In Java we could use the lock of any object to syncronize a method or block. Because classes and threads in the JVM are objects it is possible to use their locks also. You can consider, as well, the lock of an object associated with the method or block is guarding. The answers, thus, vary due to the meaning of the association term you chose. Not a good question for the exam I'm afraid. 3) B unles one of them executes System.exit(0)

Originally posted by Shan: Multithreading may be correctly accomplished in Java in which of the following ways? a)Extend the Thread class, provide an implementation for the start method and create an instance of this new subclass b)Create a class which implements the Threadable interface, and pass an instance of this class to a Thread Class constructor c)All of the above d)None of the above

The answer is D. You don't override start, you override run and there is no Threadable interface. Rather, you could implement the Runnable interface.

Originally posted by Shan:

Locking is the main mechanism for restricting simultaneous thread access to specific java code. Which of the following statement(s) are true regarding locks / monitors? a)One lock / monitor is associated with each Class b)One lock / monitor is associated with each Synchronized block c)One lock / monitor is associated with each Thread d)One lock / monitor is associated with each Object e)All of the above

This one's rather fuzzy. A and D are obviously correct. However, I believe that you could have more than one lock associated with a thread or a synchronized block. For example, let's say that a thread calls a synchronized method on an object. That thread will then obtain that object's lock. Now, suppose that, inside that method, a synchronized method is invoked on another object. The thread also gains the lock for that object. The thread has now acquired two locks. You can create a similar example for a synchronized block. Therefore, the answer should be A & D. However, I came to this solution by making an assumption about how the exam uses the word "Thread." If the exam intended a Thread to be the Java class Thread or an instance of that class, then, just like answers A & D, there would be one lock associated with a Thread. That's where the question becomes fuzzy. I would say that the answer should be A & D.

Originally posted by Shan:

With respect to User and Daemon threads: a)Daemon threads can not be destroyed b)Running User threads prevent a Java VM from exiting c)Running Daemon threads prevent a Java VM from exiting d)Daemon threads can not be grouped together e)The Java VM will exit when no non-daemon threads are running

The answer is B. You could have multiple non-daemon threads that aren't running but are "alive." Until all of these threads are terminated, the JVM will continue to run. I hope that helps, Corey [ May 31, 2002: Message edited by: Corey McGlone ]

Originally posted by Shan: With respect to User and Daemon threads: a)Daemon threads can not be destroyed b)Running User threads prevent a Java VM from exiting c)Running Daemon threads prevent a Java VM from exiting d)Daemon threads can not be grouped together e)The Java VM will exit when no non-daemon threads are running

In option b, Running User threads could call System.exit() and terminate the program and JVM. So, I think the answer should be 'e', according to JLS 12.8. I am wondering about option a: deamon threads can not be destroyed. They can not be???

JLS 12.8 Program Exit A program terminates all its activity and exits when one of two things happens: * All the threads that are not daemon threads terminate. * Some thread invokes the exit method of class Runtime or class System and the exit operation is not forbidden by the security manager.

-Shiva ps: was reviewing all the old posts regarding threads to test my threads basics

nally posted by Jasper Vader: Do we have to know the details about hat a User Thread is and what a Daemon thread is? i didn't know we had to, the book i am studying has mentioned nothing about User or Daemon in this context. I think everything you need to know about daemon and user threads is on this page already : ) So, you should understand the basic idea and pay atttention to what Corey wrote and you'll have everything you need. Cheers, Kathy "To be remembered, it must be memorable." [so when you're studying, find a way to make this stuff interesting, exciting, strange, shocking, ANYTHING that makes your brain wake up and pay attention.]