• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread locks

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are a synchronized static method and a synchronized instance method
in one class
just like this
Demo implements Runnable
{
public synchronized static void get(){
int i=0;
while (i++<100){System.out.println("static");}}
public synchronized void set(){
int i=0;
while (i++<100)
{
System.out.println("static");}
}
public void run(){}

}
I want to know if one thread invoke the get()method while another thread invoke the set() method


I have already Tested it
public Demo
{
public void main(String ...args)
final Test t=new Test();
new Thread(t){public void run(){get()}}.start();
new Thread(t){public void run(){set()}}.start();
}

but I can get the answer?
who can explain it to me ?
thanks
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say, that even though your test runs two threads, you can't guarantee that they are actually running at exactly the same time.

Mark
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe I will ask this question by another way?
we know there exists Object lock and class lock
when frist thread invoke a synchronized instance method,second thread which also will invoke another synchronized instance method from same class need to wait and execute until the frist thread released object lock

but when frist thread invoke a synchronized static method,second thread which also will invoke a synchronized instance method from same class if need to wait and execute until the frist thread released class lock or not

who can explain it to me?
thanks
[ March 31, 2006: Message edited by: Changchun Wang ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a synchronized method is called, it will try to grab the lock. If it is owned by another thread, it will wait.

A synchronized static method will use the lock of the Class object representing the class. A synchronized (instance) method, will use the object instance as the lock. These are *two* different locks, hence, calling a synchronized static method, and synchronized method, will be executed in parallel.

Henry
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:

A synchronized static method will use the lock of the Class object representing the class. A synchronized (instance) method, will use the object instance as the lock. These are *two* different locks, hence, calling a synchronized static method, and synchronized method, will be executed in parallel.

Henry


I think both synchronized (static) mehod and synchronized (instance) mehod can access static field
we know introduce synchronized this key word is to solve Sharing and exclusive problems in the thread
but if that two method which executed in parallel will result in above prblems (Sharing and exclusive problems)?
thanks !
 
reply
    Bookmark Topic Watch Topic
  • New Topic