aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threading issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threading issue" Watch "Threading issue" New topic
Author

Threading issue

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
will following code work correctly without synchroinization problems???
class shared {
static int i=0;
synchronized static void print(long id) {
S.o.p("print() called from thread "+id+", i= "+i);
i++;
}
synchronized void display(int id) {
S.o.p("display() called from thread "+id+", i= "+i);
i--;
}
}
class thread1 extends Thread {
int id = 1; // for thread2 it will be 2
shared s;
thread1(shared s) {
this.s = s;
}
public void run() {
try {
while(i<50) {
s.print(id);
Thread.sleep(100);
}
catch(Exception e) {
}
}
}
// assume similar code for thread2 class having id = 2 and calling display() method on 's' instead of s.print(id) with id = 2
class mainThread {
static void main(String[] s) {

thread1 t1 = new thread1();
thread2 t2 = new thread2();

t1.start();
t2.start();
}
}
regards,
maulin.
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
Will not compile:
public void run() {
try {
while(i<50) {
s.print(id);
Thread.sleep(100);
}
catch(Exception e) {
}
}
piece of code does not have proper set of braces.
And even if you correct that i will not be accessible in class thread1, it belongs to class shared.
Class thread1 does not have a default constructor. You cannot use thread1 t1 = new thread1();


------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)


Cheers, <img src="smile.gif" border="0"> <br /><a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">Manoj</a><br />(<a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">http://www7.brinkster.com/manoj9/</a>)
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
sorry for typo but if u consider all the braces are correct then would it work properly???
thanks!
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
sorry once more an error.
'i' is also a member var in thread1 and thread2.
sorry for so many mistakes but i hate this html editor for
writing java code that has no indentation and i cant simply copy as i work on unix.
regards,
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
consider i++ in while loops...
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
Consider using UBB to post your Java code.
Look at this piece from your own code:

See UBB usage info at: http://www.javaranch.com/ubb/ubbcode.html
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
thanks manoj but i'm seeking for the answer to my question....
regards,
maulin.
Chin Loong
Ranch Hand

Joined: Nov 19, 2001
Posts: 45
well, what i can say is, "help people to help you" ^_^. if your code is unreadable, what makes u think they'll even read it?
if your code is put nicely, it makes it easy for other people to help you
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Chin Loong:
well, what i can say is, "help people to help you" ^_^. if your code is unreadable, what makes u think they'll even read it?
if your code is put nicely, it makes it easy for other people to help you

Hi chin
here no one is helping anyone else.... every one is helping oneself...
you should try to solve the problem ... if you get it else let others do it.
Obviously it helps if readability is there .... but if question is clear then answer should be there ... and AGAIN u are not helping here anyone... you are helping yourself.
Here is problem again:


hi,
will following code work correctly without synchroinization problems???

<pre>
class Shared {
int i=0; //static
synchronized static void print(long id) { // synchronized static
System.out.println(" print() called from thread "+id+", i= "+i);
i++;
}
synchronized void display(int id) { // synchronized
System.out.println("display() called from thread "+id+", i= "+i);
i--;
}
}

class Thread1 extends Thread {
int id = 1; // for thread2 it will be 2
Shared s;
Thread1(Shared s) {
this.s = s;
}
public void run() {
try {
while(s.i<50) {
s.print(id);
Thread.sleep(100);
//Shared.i++;
s.i++;
}
}catch(Exception e) {
}
}
}
// assume similar code for thread2 class having id = 2 and calling display() method on 's' instead of s.print(id) with id = 2
class Thread2 extends Thread {
int id = 2;
Shared s;
Thread2(Shared s) {
this.s = s;
}
public void run() {
try {
while(s.i<50) {
s.display(id);
Thread.sleep(100);
s.i++;
//Shared.i++;
}
} catch(Exception e) {
}
}
}

class mainThread {
static void main(String[] args) {
Shared s = new Shared();
Thread1 t1 = new Thread1(s);
Thread2 t2 = new Thread2(s);
//t1.setPriority(Thread.MAX_PRIORITY );
t1.start();
t2.start();
/*try{
t1.join();
t2.join();
}catch(Exception e){
}
*/
}
}

</pre>
edit: modified the code
[This message has been edited by ravish kumar (edited November 21, 2001).]
[This message has been edited by ravish kumar (edited November 21, 2001).]


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi Maulin
will u elaborate ...work correctly ??
------------------
Regards
Ravish
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
okay okay okay ,
sorry once more. i wrote the code on Threads and Synchronization forum and here is the link,
http://www.javaranch.com/ubb/Forum27/HTML/000500.html
Hope this code is clear now to all.
and I agree there were problems with my code Chin but i apologized for that. i would not be expecting anyone to correct my code so much being a s/w developer.
sorry once again.
maulin.
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
I just posted a reply to your post in the other forum. Get it there.
------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
Though I still wonder what you are trying to do with this?
------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Maulin, Vasavada:

i would not be expecting anyone to correct my code so much being a s/w developer.
maulin.

sorry Maulin
I edited the code which says // assume similar code for thread2 class having id = 2 and calling display() method on 's' instead of s.print(id)
and if you want to compile it ..... do not complain and give answer...( i'm seeking for the answer to my question.. ) though might U have to change every bit of code.
but do not dare to touch my code...... I am DEVELOPER...
I apoliges Maulin, and Chin from you too .... you were on right track ...
but still can you Explain what do you wannt from us regarding this code
Originally posted by Manoj Gupta:

Though I still wonder what you are trying to do with this?
------------------



------------------
Regards
Ravish
Chin Loong
Ranch Hand

Joined: Nov 19, 2001
Posts: 45
Hi ravish,
sorry for saying that. i'm too used being a regular at forums where we help people solve their problems . i forgot this is a SCJP certification forum where newbies are expected to answer to help themselves. forgive me for my please-be-clear-if-you-want-us-to-help-to-make-our-lives-easier-because-we're-giving-a-free-service mentality ^_^
sorry

[B] Hi chin
here no one is helping anyone else.... every one is helping oneself...
you should try to solve the problem ... if you get it else let others do it.
Obviously it helps if readability is there .... but if question is clear then answer should be there ... and AGAIN u are not helping here anyone... you are helping yourself.

Chin Loong
Ranch Hand

Joined: Nov 19, 2001
Posts: 45
and look! my horn's green! hmm i think my hoof's green too...
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Originally posted by Chin Loong:
and look! my horn's green! hmm i think my hoof's green too...


Hi all,
well. javaranch has been greate support for me since i started looking at these mails. really. i submitted this code to make myself clear about synchronization fundametals. Class lock which is obtained for static methods and object locks that we get for instance methods needs some attention.
if we do things that i had in code,
i was using static variable in static and instance methods (print() and display()) then Java can't do anything if there is synchronization problem. that would be a programmers error while writing a code. java gurantees that two threads cant access any class method if one of them has class lock but it allows object methods (instance methods) to be executed by other threads. so if we dont take care that we are not using "static" variables in my instance methods then it would cause problems.
thats it. i was trying to make myself more clear about my fundamentals if i was wrong. probably this seemed not question asked in scjp but i was not sure whether one can ask such sort of question in the exam or not.
thanks again u all so much. i appreciate all the replies. i will try my best to write perfect code in future to prevent confusions.
regards
maulin.
 
Consider Paul's rocket mass heater.
 
subject: Threading issue