This topic is 2531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm just learning multithreading, could someone please explain why i get different outputs each time I run this? I understand that the three Free objects in the main method are running on separate threads at the same time, which is why the hello worlds show up first in the console. However, why do sometimes they all show up before the outputs from the Free class and sometimes one will be first and the others after it prints Hello 0, etc. Why is it different every time? i know it probably doesnt make much difference but could someone just clear it up for me?

Thanks for any help!!

 public class Free extends Thread{ @Override public void run() { for(int i = 0; i<10; i++){ System.out.println("Hello " +i); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class Practice { public static void main (String args []){ Tester free = new Free(); Free free2 = new Free(); Free free3 = new Free(); free.start(); free2.start(); free3.start(); System.out.println("Hello World"); System.out.println("Hello World"); System.out.println("Hello World"); System.out.println("Hello World"); } } 

##### Share on other sites

I'm just learning multithreading, could someone please explain why i get different outputs each time I run this? I understand that the three Free objects in the main method are running on separate threads at the same time, which is why the hello worlds show up first in the console. However, why do sometimes they all show up before the outputs from the Free class and sometimes one will be first and the others after it prints Hello 0, etc. Why is it different every time? i know it probably doesnt make much difference but could someone just clear it up for me?

No, they are not running at the same time. There's no guarantee threads are executing at the same time, or any time at all. Thread scheduling is done by the OS and you can't control that. The system might decide to run thread #3 first, then abruptly suspend it, and run thread #1 and #2 till they are done, then continue to #3.

##### Share on other sites
ok thanks! I thought that the thread would start at the same time every time i ran the program. Looks like i was wrong, so makes sense now. thanks again bud

##### Share on other sites
Just one more question on the topic. in the same book on android games the author uses:

 public void whatEver(){ Synchronized(this){ //Whatever }} 

isnt that the same as simply writing it like this?

public synchronized void whatEver();

I learned that if you pass an object into synchronized; threads will move onto another method while the the currrent method is being used by another thread, but after experimenting that does not happen when you pass in the keyword this. So main question, are the two above the same? if so then why would the author chose to write all the methods this way? Thanks!

P.S. sorry if this sounds dumb, still really new to programming and probably trying to hop into games to fast :/

##### Share on other sites
If you are new to programming the very last thing you should be doing is thinking about multi-threading. Seriously - I do not mean to put you down, even advanced programmers have difficulty designing multi-threaded systems that are correct.

As for your question, by using the synchronized keyword inside the function body, one can be more explicit about the object being locked*. You can also reduce the area being locked to less than the entire function. Typically, you might only have a couple of lines that need co-ordination.

And no, threads don't move onto another method while the lock is held. Each thread blocks on the lock, causing them to stop running until the thread using the lock has relinquished it.

Even advanced programmers try to use higher level patterns (such as futures, thread pools, concurrent data structures) rather than trying to write things in terms of low level locking. For this reason, again I would advise you write non-multi-threaded programs first. The book is perhaps too advanced for you at the moment.

* [size=2]Locking an arbitrary, publicly visibly object is dangerous and can lead to deadlocks if not handled carefully. A common idiom is to have a private final Object lock = new Object(), and synchronise on that.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5
A4L
10

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633711
• Total Posts
3013488
×