Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-20-2007, 04:51 PM
Member
 
Join Date: Nov 2007
Posts: 1
blowguy is on a distinguished road
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.

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 05:06 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2007, 07:19 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,017
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing text encoding talgreen Eclipse 0 03-30-2008 09:14 PM
Performance issues related to trigonometric functions , floating point arithmetic and JavaForums Java Blogs 0 02-04-2008 12:40 PM
IOExceptions and missing JavaDoc tim NetBeans 0 01-29-2008 01:55 PM
fetching document content and MIME_TYPE through share-point connector API suresh72 Advanced Java 0 12-19-2007 12:47 PM
How to read the MAC of an Access Point goodjonx Networking 2 12-19-2007 12:31 PM


All times are GMT +3. The time now is 11:44 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org