Results 1 to 4 of 4
  1. #1
    java girl is offline Member
    Join Date
    Jan 2009
    Rep Power

    Default sychronized data type

    GooD EveninG ..

    I want to ask if there is any way to make any data type sychronized (such as int, linkedlist .. ) ?

    I know there is a way to sychronize a method or statement .. but I don't have any idea about data type .. ?

    I hope you have an idea about that ..

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    One way is to mark the field "volatile". However, this does not really accomplish what you want when dealing with a list.

    The best way, IMHO, to synchronize an instance field (one defined outside a method) is to create a second field of type Object.

    private final Object myFieldSync = new Object();

    When you access your field in a way that you need synchronization, wrap the code in a synchronized block.
    Java Code:
    synchronized(myFieldSync) {
      \\ do things with myField, this can involve several statements
    What I see as the big problem with volatile and the new synchronized wrapper classes is that they don't guarantee you have exclusive use of the object for several statements. That is especially important when dealing with a list object, because a list iterator will throw an exception if the list is modified.

  3. #3
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Rep Power


    Check java.util.concurrent package. it has lot of good concurrency stuff from Java 5 only....

    There are lot of atomic classes added in Java 5, good to read about those implementations...

    Music | HTS | Remix
    Live life king size

  4. #4
    l_m_b is offline Member
    Join Date
    Feb 2009
    Rep Power


    Hello, java girl!

    No, there is no generally any way to make a data type synchronized. Synchronizing a complex data type might be very tricky. Anyway there are many basic concurrent data structures (lists, maps, queues, sets, etc) shipped with Java that may suit almost all your needs.

    You may check java.util.concurrent package.

    You may check java.util.Collections class which has methods:

    static List synchronizedList(List list)
    static Map synchronizedMap(Map m)
    static Set synchronizedSet(Set s)

    A good book on concurrency I would always recommend is “Java Concurrency in Practice”.

    As for volatile variables they are more about visibility than synchronization. Here is a typical use case for a volatile variable. The code below is wrong:

    Java Code:
    public class Test 
    	private static boolean ready;
    	private static class ReaderThread extends Thread 
    		public void run() 
    			while (!ready)
    		private void doSomeWork()
    	public static void main(String[] args) {
    		new ReaderThread().start();
    		ready = true;
    It may end with infinite loop on some JVMs. That is because ready is nonvolatile.

Similar Threads

  1. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM
  2. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  3. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  4. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  5. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 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