Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: synchronization

  1. #1
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default synchronization

    hi could someone please help me understand how interference might occur in this program ?

    class ThreadTest implements Runnable {
    static String printString = null;
    private String threadName;
    ThreadTest(String s) { threadName = s; }
    public void run() {
    int i = 0;
    while (i < 100) {
    if (printString == null) {
    printString = threadName + (++i);
    } else {
    System.out.println(printString);
    printString = null;
    }
    }
    }
    public static void main(String[] args) {
    Thread t1 = new Thread(new ThreadTest("a"));
    Thread t2 = new Thread(new ThreadTest("b"));
    t1.start();
    t2.start();
    }
    }

    its meant to print a1 to a100 and b1 to b100...


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: synchronization

    I think you first need to understand concept of threads very clearly........
    lets try this way
    if the output is as you expected....what's the use of setting priority of a thread?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: synchronization

    its meant to print a1 to a100 and b1 to b100...
    What does it currently print when its executed? Do you expect something else to print?
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. problem in synchronization
    By codenoob in forum Threads
    Replies: 3
    Last Post: December 8th, 2011, 08:29 AM
  2. Synchronization on object
    By god1gracious in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 01:13 PM
  3. Synchronization of ServletContext Object
    By jkoder70014 in forum Java Servlet
    Replies: 2
    Last Post: August 23rd, 2011, 10:58 AM
  4. synchronization problems
    By stroodlepup in forum Threads
    Replies: 2
    Last Post: February 28th, 2011, 09:26 AM
  5. synchronization
    By bbr201 in forum Java Theory & Questions
    Replies: 6
    Last Post: August 29th, 2010, 09:32 AM