Increment/Decrement/Print using multiple threads(Code review)
I was trying to write a code where multiple threads call methods on a shared object to increment/decrement/print a counter stored in it. What I also want are these numbers to vascillate between 0 and 8. That is the output might look like something below:
0123234567654566677877666655....

Can somebody look at what I have cranked out and give me some pointers on whether I am on the right track:

My shared object:

    public class SyncObj{
            private int i;
            public synchronized void inc(){
                    if(i<8)
                      i++;
            }
            public synchronized void dec(){
                    if(i > 0)
                       i--;
            }
            public synchronized void print(){
                    System.out.print(i);
            }
    }

To prevent starvation on the print and to make sure every inc/dec gets printed, I can have a private variable called hasPrinted and rewrite the class as follows:

    public class SyncObj{
                private int i;
                private Boolean hasPrinted = false;
                public synchronized void inc(){
                        if(i<8 && hasPrinted){
                          i++;
                          hasPrinted = false;
                        }
                }
                public synchronized void dec(){
                        if(i > 0 && hasPrinted){
                           i--;
                           hasPrinted = false;
                        }
                }
                public synchronized void print(){
                        System.out.print(i);
                        hasPrinted = true;
                }
        }

Can somebody go over the above snippet and review it for pitfalls and gotchas?

Thanks
Increment/Decrement/Print using multiple threads(Code review)
===

I was trying to write a code where multiple threads call methods on a shared object to increment/decrement/print a counter stored in it. What I also want are these numbers to vascillate between 0 and 8. That is the output might look like something below:
0123234567654566677877666655....

Can somebody look at what I have cranked out and give me some pointers on whether I am on the right track:

My shared object:

    public class SyncObj{
            private int i;
            public synchronized void inc(){
                    if(i<8)
                      i++;
            }
            public synchronized void dec(){
                    if(i > 0)
                       i--;
            }
            public synchronized void print(){
                    System.out.print(i);
            }
    }

To prevent starvation on the print and to make sure every inc/dec gets printed, I can have a private variable called hasPrinted and rewrite the class as follows:

    public class SyncObj{
                private int i;
                private Boolean hasPrinted = false;
                public synchronized void inc(){
                        if(i<8 && hasPrinted){
                          i++;
                          hasPrinted = false;
                        }
                }
                public synchronized void dec(){
                        if(i > 0 && hasPrinted){
                           i--;
                           hasPrinted = false;
                        }
                }
                public synchronized void print(){
                        System.out.print(i);
                        hasPrinted = true;
                }
        }

Can somebody go over the above snippet and review it for pitfalls and gotchas?

Thanks