This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Will this work correctly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply locked New topic
Author

Will this work correctly?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi all,
will following code work correctly w/o synchronization problems?
class shared {
static int i=0;
synchronized static void print(long id) {
S.o.p("print() called from thread id :"+id+", i ="+i);
i++;
}
synchronized void display(int id) {
S.o.p("display() called from thread id :"+id+", i = "+i);
i--;
}
}
class thread1 extends Thread {
int i=0,id=1;
shared s;
thread1(shared s) {
this.s =s;
}
public void run() {
try {
while( i < 50 ) {
Thread.sleep(100);
s.print(id);
i++;
}
} catch(Exception e) {
}
}
}
//
class thread2 extends Thread {
int i=0,id=2;
shared s;
thread2(shared s) {
this.s =s;
}
public void run() {
try {
while( i < 50 ) {
Thread.sleep(100);
s.display(id);
i++;
}
} catch(Exception e) {
}
}
}
//
class mainThread {
static void main(String[] s) {
shared s = new shared();
thread1 t1 = new thread1(s);
thread2 t2 = new thread2(s);

t1.start();
t2.start();
}
}

if i'm missing and braces or if there is some syntax error correct it and then consider the code that is able to compile. 'coz i ran this code but i may 've syntax error while typing it over here.
regards,
maulin
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
I made a few changes to your code and it looks like this now.
class shared {
static int i=0;
synchronized static void print(long id) {
System.out.println("print() called from thread id :"+id+", i ="+i);
i++;
}
synchronized void display(int id) {
System.out.println("display() called from thread id :"+id+", i = "+i);
i--;
}
}
class thread1 extends Thread {
int i=0,id=1;
shared s;
thread1(shared s) {
this.s =s;
}
public void run() {
try {
while( i < 50 ) {
Thread.sleep(100);
s.print(id);
i++;
}
} catch(Exception e) {
}
}
}
//
class thread2 extends Thread {
int i=0,id=2;
shared s;
thread2(shared s) {
this.s =s;
}
public void run() {
try {
while( i < 50 ) {
Thread.sleep(100);
s.display(id);
i++;
}
} catch(Exception e) {
}
}
}
//
public class TestA {
static void main(String[] args) {
shared s = new shared();
thread1 t1 = new thread1(s);
thread2 t2 = new thread2(s);
t1.start();
t2.start();
}
}
This code works and keeps printing:
print() called from thread id :1, i =0
display() called from thread id :2, i = 1
print() called from thread id :1, i =0
display() called from thread id :2, i = 0
display() called from thread id :2, i = 0
print() called from thread id :1, i =-1
display() called from thread id :2, i = 0
print() called from thread id :1, i =0
display() called from thread id :2, i = 0
print() called from thread id :1, i =-1
display() called from thread id :2, i = 0
print() called from thread id :1, i =-1
display() called from thread id :2, i = 0
print() called from thread id :1, i =-1
display() called from thread id :2, i = 0
print() called from thread id :1, i =0
display() called from thread id :2, i = 0
print() called from thread id :1, i =-1
display() called from thread id :2, i = 0
print() called from thread id :1, i =0
print() called from thread id :1, i =0
display() called from thread id :2, i = 1

------------------
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>)
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
I see you posted this question to Programmer Certification as well. Fortunately Manoj is active in both forums otherwise he probably would have wasted quite a bit of his time duplicating answers already given there. For that reason, please don't post your questions in more than one forum. Thanks.
- Peter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Will this work correctly?
 
Similar Threads
instance members, shared?
Thread example, dont know why is it a deadlock condition
Threading issue
locking??
Need Explanation of unpredicted output even if methods are synchronized