Results 1 to 2 of 2
  1. #1
    raghusomineni is offline Member
    Join Date
    Apr 2017
    Posts
    2
    Rep Power
    0

    Default I need help to rectify problem in my multithreading program

    Hi to all.

    I'm learning Java. Now learning multi-threading programming concepts. I wrote a program using synchronized keyword. It is executing and showing output but it is wrong output and also I'm getting NullPointerException in my output. I'm using Eclipse Neon IDE. The code is below.

    Java Code:
    package myPackage;
    
    class Callme
    {
    	private String message;
    
    	Callme()
    	{
    	}
    	Callme(String message)
    	{
    		this.message = message;
    	}
    	
    	synchronized void call()
    	{
    		System.out.print("[" + message);
    		try
    		{
    			Thread.sleep(1000);
    		}
    		catch (InterruptedException ie)
    		{
    			System.out.println("Interrupted");
    		}
    		System.out.println("]");
    	}
    	String getMessage()
    	{
    		return (message);
    	}
    }
    
    class Caller implements Runnable
    {
    	private String message;
    	private Callme target;
    	private Thread t;
    
    	Caller(Callme target)
    	{
    		this.target = target;
    		message  = target.getMessage();
    		t = new Thread(this);
    		t.start();
    	}
    	Caller(Callme target, String message)
    	{
    		this.target = target;
    		this.message = message;
    		Thread t = new Thread(this);
    		t.start();
    	}
    
    	public void run()
    	{
    		target = new Callme(message);
    		target.call();
    	}
    	Thread getThread()
    	{
    		return (t);
    	}
    }
    
    class SynchronizedDemo
    {
    	public static void main(String[] args)
    	{
    		Callme target = new Callme();
    		Caller ob1 = new Caller(target, "Java");
    		Caller ob2 = new Caller(target, "Synchronized");
    		Caller ob3 = new Caller(target, "Keyword");
    
    		/*Callme target1 = new Callme("Java");
    		Callme target2 = new Callme("Synchronized");
    		Callme target3 = new Callme("Keyword");
    
    		Caller ob1 = new Caller(target1);
    		Caller ob2 = new Caller(target2);
    		Caller ob3 = new Caller(target3);*/
    
    		try
    		{
    			(ob1.getThread()).join();
    			(ob2.getThread()).join();
    			(ob3.getThread()).join();
    			
    			System.out.println("ob1 thread is alive: " + (ob1.getThread()).isAlive());
    			System.out.println("ob2 thread is alive: " + (ob2.getThread()).isAlive());
    			System.out.println("ob3 thread is alive: " + (ob3.getThread()).isAlive());
    		}
    		catch (InterruptedException ie)
    		{
    			System.out.println("Main thread interrupted");
    		}
    	}
    }
    The Output may be like below.

    [Java]
    [Synchronized]
    [Keyword]

    (OR)

    [Synchronized]
    [Java]
    [Keyword]

    The output may be different every time. Here we may not get exact output every time.

    But, if I run this code I'm getting below output in Eclipse.

    Exception in thread "main" [Java[Synchronization[Keywordjava.lang.NullPointerException
    at myPackage.Synch.main(Synch.java:85)
    ]
    ]
    ]

    Please anyone let me know where I'm mistaken and correct me.

    Thank you all.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: I need help to rectify problem in my multithreading program

    Here's a hint. The problem lies in your Caller class. To help figure this out, you need to use some print statements in analyzing Caller methods.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. a multithreading problem
    By esolve in forum Advanced Java
    Replies: 1
    Last Post: 02-28-2017, 12:13 AM
  2. Am getting error for the following program. Kindly rectify.
    By gangaprabakaran in forum New To Java
    Replies: 5
    Last Post: 03-21-2016, 06:14 PM
  3. MultiThreading Problem
    By ravjot28 in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 12:23 PM
  4. MultiThreading Chat Program
    By Noelf21 in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-23-2009, 10:49 AM
  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
  •