1

I have a multithreaded application in Java. In the run method of one of the threads, I have a

System.out.println("Print something something something")
statement. The problem I have is that sometimes, the output I get from this print is not "Print something something something" but "something Print something something" and some other variations. Please what is the cause of this? The method I am calling in my run method is shown below.
public synchronized void generateRequests() {

        String name;
        int qty;
        int index = 0;
        System.out.print("Customer " + custId + " requests, ");
        for (Item i : items) {
            name = i.getName();
            qty = randNoGenerator(0, 10);
            items.set(index, new Item(name, qty));
            index++;
            System.out.print(qty + " " + name + ", ");
        }

        s.serviceRequests(this);
    }
Yanki Twizzy
  • 7,771
  • 8
  • 41
  • 68
  • No, show us your code. That would not happen as is. – Sotirios Delimanolis Mar 16 '14 at 18:48
  • Since there's no code, I'm going to guess that you actually have multiple threads, each of which prints one word. In that case, the threads will run in an unpredictable order, and so the order in which the words are printed will vary from run to run. – dlev Mar 16 '14 at 18:54
  • @SotiriosDelimanolis I have added some code. Hope it helps – Yanki Twizzy Mar 16 '14 at 18:59
  • Explain what your expectations are and what actually happens. A small reproducible example would be best. – Sotirios Delimanolis Mar 16 '14 at 19:00
  • I have 5 threads that use the above method to generate requests and also to printout the generated request. The requests are generated appropriately but the problem is that it does not print out what is generated sequentially as I specified. It prints at random sometimes starting with the items and not "Customer ... – Yanki Twizzy Mar 16 '14 at 19:06
  • Is this method called on single object? – Braj Mar 16 '14 at 19:54
  • Suppose Thread 1 enters in this method and prints customer data but meanwhile second Thread 2 enters in same method called by another object then it will also print customer data first and finally all the data will be mixed. – Braj Mar 16 '14 at 19:57
  • If you want it to be performed sequentially then why are you using multi threading? – Braj Mar 16 '14 at 20:42
  • You have not answered `Is this method called on single object?` – Braj Mar 17 '14 at 07:49
  • No it is not. It is called on an array of 5 objects – Yanki Twizzy Mar 17 '14 at 15:52
  • It is actually a duplicate. The answer given in the duplicate question correctly satisfies my needs – Yanki Twizzy Mar 17 '14 at 19:22

1 Answers1

1

A sample code to understand this problem.

code:

public class MultiThreading implements Runnable {

    private String id;
    private double qt;

    public MultiThreading(String id, double qt) {
        this.id = id;
        this.qt = qt;
    }

    public static void main(String[] args) {
        MultiThreading obj = new MultiThreading("1", 1000);
        for (int i = 0; i < 5; i++) {
            // Thread thread = new Thread(new MultiThreading(String.valueOf(i), (i * 1000)));
            Thread thread = new Thread(obj);
            thread.setPriority(i + 1);
            thread.start();
        }
    }

    public synchronized void generateRequests() {

        System.out.println("Customer:" + this.id);
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Quentity:" + this.qt);
    }

    @Override
    public void run() {
        generateRequests();
    }

}
  • synchronized method is called on same object (This will not mix up the output)

        MultiThreading obj = new MultiThreading("1", 1000);
        for (int i = 0; i < 5; i++) {                
            Thread thread = new Thread(obj);
            thread.setPriority(i + 1);
            thread.start();
        }
    

output:

Customer:1
Quentity:1000.0
Customer:1
Quentity:1000.0
Customer:1
Quentity:1000.0
Customer:1
Quentity:1000.0
Customer:1
Quentity:1000.0
  • synchronized method is called on different object (This will mix up the output and it may different on next run)

       for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(new MultiThreading(String.valueOf(i), (i * 1000)));
            thread.setPriority(i + 1);
            thread.start();
        }
    

output:

Customer:0
Customer:1
Customer:4
Customer:2
Customer:3
Quentity:0.0
Quentity:2000.0
Quentity:3000.0
Quentity:1000.0
Quentity:4000.0
Braj
  • 46,415
  • 5
  • 60
  • 76
  • You came close but my issue is that instead of having something like this Customer:0 Customer:1 Customer:4 Customer:2 Customer:3 Quentity:0.0 Quentity:2000.0 Quentity:3000.0 Quentity:1000.0 Quentity:4000.0 I have Customer:0 :1 Customer Customer:4 Customer:2 3:Customer Quentity:0.0 Quentity:2000.0 3000 Quentity: Quentity:1000.0 Quentity:4000.0 – Yanki Twizzy Mar 17 '14 at 00:23
  • I am unable to reproduce it. Please can you share your complete code? – Braj Mar 17 '14 at 08:20