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 4 of 4

Thread: what is wrong in this code ??? I want out put as.. Hello! I am here ...but it is providing Hello! here am I

  1. #1
    Junior Member
    Join Date
    May 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is wrong in this code ??? I want out put as.. Hello! I am here ...but it is providing Hello! here am I

     
    package general;
    class SharedObject{
    	void SharedMethod(String arg){
    		System.out.print("[");
    		System.out.print(arg);
    		try{
    			Thread.sleep(1000);
    		}
    		catch(InterruptedException e){
    			System.out.println("Interrupted"+e);
    		}
    		System.out.println("]");
    	}
     
    }
     
    class Thread1 implements Runnable{
     
    	String arg;
    	SharedObject obj1;
    	Thread t;
    	public Thread1(SharedObject obj1, String arg){
    		this.arg=arg;
    		this.obj1=obj1;
    		t=new Thread(this);
    		t.start();
     
    	}
    	public void run(){
    		synchronized(obj1){
    		obj1.SharedMethod(arg);
    	}
    		}
    }
    class SynchEx1 {
    public static void main(String [] args){
    	SharedObject obj1=new SharedObject();
    	Thread1 t1=new Thread1(obj1, "Hello!  ");
    	Thread1 t2=new Thread1(obj1, "I");
    	Thread1 t3=new Thread1(obj1, "am");
    	Thread1 t4=new Thread1(obj1, "here");
     
     
    	try{
    		t1.t.join();
    		t2.t.join();
    		t3.t.join();
    		t4.t.join();
     
    	}catch(Exception e){
    		System.out.println(" main Thread Interrupted");
    	}
     
    }
    }

    // here my concern is, I created thread t1, t2, t3, t4 in this order and t1 gets executed first because Here I am using synchronized block but after t1 gets completed it should go for t2 then t3 and so on but it executes first t1 after that last one then second last and so on....
    So Please do let me know is there anything I am missing here or it is the problem of OS??
    Last edited by Tapas; May 28th, 2017 at 12:36 PM.

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: what is wrong in this code ??? I want out put as.. Hello! I am here ...but it is providing Hello! here am I

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    How does the code try to control the order that the methods are executed in?
    The OS will make choices that are hard to control.

  3. #3
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: what is wrong in this code ??? I want out put as.. Hello! I am here ...but it is providing Hello! here am I

    Try adding some print statements to the code to show more.
    I found that adding a print statement in the run() method before the synchronized statement changed the order of the print out.

    after t1 gets completed it should go for t2 then t3
    Why do you think the OS should choose t2 after t1 finishes? I don't know how the OS decides what thread to execute next.

  4. #4
    Junior Member
    Join Date
    May 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong in this code ??? I want out put as.. Hello! I am here ...but it is providing Hello! here am I

    I can't seem to find any mistake in the code but as you say there is one i tend to ask you for more information about what really happens. There are tools which help detecting errors within the code, such as checkmarx and others. It is recommended though, to find it on your own.
    Good luck. Ben.

Similar Threads

  1. Replies: 5
    Last Post: October 4th, 2013, 11:27 AM
  2. Providing Security to run
    By m.vinay09@gmail.com in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: June 28th, 2013, 12:18 AM
  3. Class Extends JFrame and implements Runnable - Where to put the code
    By mds1256 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 12th, 2013, 08:51 AM
  4. How to put my code onto the window.
    By Shzylo in forum Java Theory & Questions
    Replies: 1
    Last Post: December 18th, 2012, 08:34 PM
  5. Beginner - Where to put code to connect to database
    By newbiee in forum JDBC & Databases
    Replies: 1
    Last Post: May 17th, 2012, 05:40 AM