Results 1 to 2 of 2
  1. #1
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default a multithreading problem

    Java Code:
    package misc;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    class Elem {
    	private String name;
    	private volatile int value = 0;
     
    	Elem(String name) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setValue(int value) {
    		this.value = value;
    	}
    
    	public int getValue() {
    		return value;
    	}
    
    
    }
    
    class MyThread implements Runnable {
    
    	Elem e = new Elem("Wx");
    	
    	@Override
    	public void run() {
    		synchronized (e) {
    			if (e.getValue() == 0) {
    				try {
    					e.setValue(1);
    					System.out.println(Thread.currentThread().getName() + " starts waiting!");
    					e.wait(2000);
    					System.out.println(Thread.currentThread().getName() + " stops waiting!");
    					e.setValue(0);
    				} catch (InterruptedException e1) {
    					e.setValue(0);
    					e1.printStackTrace();
    				}
    			}
    			
    			else{
    				System.out.println(Thread.currentThread().getName() + " starts sleeping!");
    				try {
    					Thread.sleep(2000);
    					System.out.println(Thread.currentThread().getName() + " stops sleeping!");
    				} catch (InterruptedException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				
    			}
    
    		}
    	}
    }
    
    public class Temp {
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new MyThread());
    		Thread t2 = new Thread(new MyThread());
    		t1.setName("Thread One");
    		t2.setName("Thread Two");
    		t1.start();
    		t2.start();
    	}
    }
    the result is

    Java Code:
    Thread One starts waiting!
    Thread Two starts waiting!
    Thread One stops waiting!
    Thread Two stops waiting!
    I expect it to be

    Java Code:
    Thread One starts waiting!
    Thread Two starts sleeping!
    Thread Two stops sleeping!
    Thread One stops waiting!
    where am I wrong?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: a multithreading problem

    One problem I see if you expect synchronized() to work between threads is the synchronized statements do not use the same object.

    Explain why you expect that output.
    Last edited by Norm; 02-28-2017 at 12:21 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 11-13-2015, 04:25 PM
  2. Java Multithreading Problem
    By avirit1983 in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-03-2010, 10:18 PM
  3. MultiThreading Problem part 2
    By ravjot28 in forum New To Java
    Replies: 33
    Last Post: 03-11-2010, 06:22 PM
  4. MultiThreading Problem
    By ravjot28 in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 12:23 PM
  5. problem using ObjectOutputStream in multithreading
    By sanjeevbindroo in forum Networking
    Replies: 3
    Last Post: 10-15-2009, 08:00 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
  •