Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

thread  RSS feed

 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

If we have a synchronized method say
Class A
{
public void synchronized methodA(){ }
}
this.methodA();
They say only one thread can acess this at a time .But its actually an object thats accessing the code.Can anyone clear me.How can a thread access this method.Does an object have many threads

bye
nitin
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin,
If you don't do anything special your object is being called by only one thread (the main one.) If you create other threads, they could both have different objects or references to the same object of a certain class. The different threads could be calling the method at the same time.
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

What I mean to say a thread is accesing a method.Does that mean the object which is used to acess this method has threads.Or some other external means

bye
nitin
 
Vali GB
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i am not sure if i got your question right but i will give it a try anyway:

the idea of threads is that any running program at any given time consists of one thread at least, i.e the public static void main method in java.
this main method will be in charge of starting your program, running it and eventually starting other threads.
Now, every piece of code like

will be actually run by the current thread (main or your personal Thread instance) (there's a method in Thread that returns the current thread :

The only purpose for synchronized methods is that it guarantees that only one thread ( be it main or any Thread object that you'll instantiate) can access this method at a given time.

In conclusion, it's always a thread that executes your code even if it seems that another object tries to run it (like in the previous example)
[ June 06, 2004: Message edited by: Vali GB ]
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vali,

I think u are similar to what Im thinking.Here it says every thread is an instance of java execution engine.I got this piece of information from this link hope this helps

http://www.artima.com/insidejvm/ed2/jvm10.html

Each thread of a running Java application is a distinct instance of the virtual machine's execution engine. From the beginning of its lifetime to the end, a thread is either executing bytecodes or native methods. A thread may execute bytecodes directly, by interpreting or executing natively in silicon, or indirectly, by just- in-time compiling and executing the resulting native code. A Java virtual machine implementation may use other threads invisible to the running application, such as a thread that performs garbage collection. Such threads need not be "instances" of the implementation's execution engine. All threads that belong to the running application, however, are execution engines in action.

Still can you look what he actually means

bye
nitin
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Vali!

_______________________________________________________________________

Think of a thread as a Program Counter and a stack independent of the others threads. When a method invokes another method the stack frame of the called method is pushed in the stack of the thread executing the calling method. When a method returns, its frame is taken out of the stack, and the stack frame of the calling method becomes the current frame again.
A thread shares main memory with all others threads, it is called a lightweight process.

It's better to think of threads as a sequence of method invocations, or something capable of invoking methods in objects. With two threads, two methods could be running "simultaneously"; that is interleaved, in a single CPU machine; for instance, we could maintain GUI resposiveness for the user while a lenghty operation is carried out. If only one thread were used, the user interation with the GUI will be delayed untill the completion of the other task.
[ June 08, 2004: Message edited by: Jose Botella ]
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi botella,

It's better to think of threads as a sequence of method invocations, or something capable of invoking methods in objects.

This it seems correct.Basically the concept of thread is abstracted.Now I have another thing they are reader and writer locks for threads.

We can handle transactions by using threads or by EJBs .Which you think is better way.Correct me if Im wrong



bye
nitin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!