• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

threads

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a program(just for practise)
which will have 2 threads
1(any of them) should print 1-10
other should print 11-20(after first has finished)
(i do not want 2 use join()-but practise synchronization)

then with some modifications,
1(any of them) should print 1-10
other should also print 1-10 after 1st has finished.
The following code is not working:
class mythread extends Thread
{
counter c=new counter();
mythread(String s)
{
super(s);
}
public static void main(String args[]){
mythread t1= new mythread("a");
mythread t2= new mythread("b");
t1.start();
t2.start();
}
public void run()
{
c.inc();
}
}
class counter
{
private int i=0;
void inc()
{synchronized(this){
while(i++!=10) System.out.println(Thread.currentThread().getName()+i);
}}
}
thanx in advance
 
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an object has 2 methods with synchronized keyword,
2 methods without synchronized keyword
if a thread is executing synchronized method :
1.I thing the other threads cannot call both of the synchronized methods,right??
2.Can other threads call unsynchronized methods on the same object?
3.Can other threads call variables of this object?
Thanx in advance
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by leena rane:
an object has 2 methods with synchronized keyword,
2 methods without synchronized keyword
if a thread is executing synchronized method :
1.I thing the other threads cannot call both of the synchronized methods,right??
2.Can other threads call unsynchronized methods on the same object?
3.Can other threads call variables of this object?
Thanx in advance


1. right
2. yes other threads may invoke unsynchronized methods
3. yes other threads may reference variable of this object
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


....The following code is not working:.....


Ur given code is not working as per u wanted , becoz two Threads call Inc() method on diff Objects of Counter class , and thus requring diff locks . Only a single-mutually exclusive lock can help u achieve wht u want to . Here two instance , diff , of counter class are being made , which hav two diff locks .
Lemme try to be more clear
line mythread t1= new mythread("a");
creates a mythread Object , whose insatnce varible c is intialized with a Counter-class Instance , lets call this instance Object as
COUNTER1
Similarly , line mythread t2= new mythread("b");
creates a mythread Object , whose insatnce varible c is intialized with a another Counter-class Instance , lets call this instance Object as
COUNTER2
And , lets name the corresponding locks of these objects as :
LOCK_OF_COUNTER1 & LOCK_OF_COUNTER2
Now , lines
t1.start();
t2.start();

instructs the JVM to spawn 2 new Threads , lets call them as :
THREAD1 & THREAD2
Now , when run() method of THREAD1 calls c.inc() , its calling inc() method upon COUNTER1 , and when this call reaches the line synchronized(this) , THREAD1 requires to obtain LOCK_OF_COUNTER1 .
Simlilarly , when run() method of THREAD2 calls c.inc() , its calling inc() method upon COUNTER2 , and when this call reaches the line synchronized(this) , THREAD2 requires to obtain LOCK_OF_COUNTER2.
As LOCK_OF_COUNTER1 & LOCK_OF_COUNTER2 are entirely diff , and THEAD1 need to seek LOCK_OF_COUNTER1 only , THREAD2 need to seek LOCK_OF_COUNTER2 only , there is no-such synchronisation , as u wanted .

Following code share the same object c between two threads , and thus the same lock :
( it shld do , wht u wanted to )


This another code , is a much better way to achieve our-required functionality

Try both of these code , i guess ,these shld make my point clear.

About ur other queries , Val is more than 100% correct , as always


------------------
Gagan (/^_^\) SCJP2
Die-hard JavaMonk -- little Java a day , keeps u going .
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Second-last line of final code ( body of synchronized-block ) , should be read as :
for(int j=i;j <= i+9;j++)
System.out.println(Thread.currentThread().getName()+j);

I think , i typed correct , how come its displayed being garbled up ? anyway
again it garbled up??
( oh got it !! dat less than sign was doin it i guess , i changed it to less than equal to ... letc try..
------------------
Gagan (/^_^\) SCJP2
Die-hard JavaMonk -- little Java a day , keeps u going .
[This message has been edited by Gagan Indus (edited October 06, 2001).]
 
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx val,
Thanx Gagan,
Gagan,recently on one of the webpages ,i read the chat of scjp wannabes and Pradeep(@Whiz..)There i saw a name Gagan.I wondered for a second is it Gagan Indus(it only said Gagan)
And then i saw da,dat,.....
and then i said "Oh it's our Gagan!! "
So i think i would be able 2 recognize u anywhere with the help of da words like dat
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U caught me , leena !!!
DAT indeed was me ! lol
( did u noticed from DA/DAT , or did u noticed me frquently using -> ,annoying other guyz ? either way its cool ! )
u must hav been like : oh no ! not here ! , rite ? lol
u r DA smart fellow !
( i hope i 'll also be able to catch U everywhere , coz of ur unique sounding name , n DA way u spell it in lower-alphabets )
felt very gud , DAT u hav noticed DA minute-stuff like DAT/DA of mine
BTW , today i came across one of our ol' discussion , last part of which i missed. Where u hav wished me luck for exam , thankx for dat
and u have asked about da court case ! bout using copyrighted smily of mine (/^_^\) !
well , we can negotiate here only no? lol
okay , i 'll append my copyright ! u r free to use it ! But then u gotta do it also ! i mean DA smile

------------------
Gagan (/^_^\) SCJP2
Die-hard JavaMonk -- little Java a day , keeps u going .
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic