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

Will this work correctly?

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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/)
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
    Bookmark Topic Watch Topic
  • New Topic