Results 1 to 2 of 2
  1. #1
    blowguy is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default Problem with synhronization.

    Hello to all posters. :)

    I am quite new to threads, so I need a little bit of Your help.

    From what I understand, keyword synchronized is for defining a part of code(statement or method) that can be executed only by one thread at the same time. Other threads which will be competing for synchronized code must wait till previous thread has completed it.

    I have a synchronized method mySwap(), which is actually executed by two threads at the same time(Pavediens1 and Pavediens2). And an error message is displayed in the console.

    Java Code:
     class uzd11 
     {
         public static int a, b;
         public  static boolean entered;
         
        static class myThread extends Thread
        {
            myThread()
            {
                
            }
            synchronized void myswap()//two threads at the same time are executing this method, but i want that if this metdod is executed by one thread, other will wait
            {
                //an error message check - we want only one thread to be executing this method
                if (entered == true) System.out.println("Error: Threads actually are not synchronized");//this error message is always displayed.
                
                entered = true;//while we are in this method this flag is true
                
                int tmp = a;
                a = b;
                b = tmp;
                
                entered = false;//exiting method - flag ir false
            }
            public void run() 
            {
               
                    for (int i = 0; i < 1900000; ++i)
                    {
                        myswap();//call synchronized method
                    }
                
    
            }
        }
    
        public static void main(String args[]) 
        {
            
            a = 2;
            b = 3;
            
            //two synchronized threads
            Thread pavediens1 = new myThread();
            Thread pavediens2 = new myThread();
            
            pavediens1.start();
            pavediens2.start();
            
                    
            try {// lets wait for threads to merge
              pavediens1.join();
              pavediens2.join();
           } catch (InterruptedException ignore) {}
            
            //Just make sure, that threads have merged...
            if (pavediens1.isAlive()) System.out.println("Pavediens 1 is Alive");
            if (pavediens2.isAlive()) System.out.println("Pavediens 1 is Alive");        
    
            //---===---
            System.out.println(a);
            System.out.println(b);
        }
    
    }
    CONSOLE: Error: Threads actually are not synchronized

    Am I missing the point what is synchronized code? Or is it bug in Java?
    Last edited by blowguy; 11-20-2007 at 04:06 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Each instance of the class is calling its own method. To demonstrate synchronized method calls the method needs to be in another class, separate from each thread instance. Then the two threads will take turns accessing the same method.

Similar Threads

  1. Missing text encoding
    By talgreen in forum Eclipse
    Replies: 0
    Last Post: 03-30-2008, 08:14 PM
  2. IOExceptions and missing JavaDoc
    By tim in forum NetBeans
    Replies: 0
    Last Post: 01-29-2008, 12:55 PM
  3. Replies: 0
    Last Post: 12-19-2007, 11:47 AM
  4. How to read the MAC of an Access Point
    By goodjonx in forum Networking
    Replies: 2
    Last Post: 12-19-2007, 11:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •