Solved

thread creation

Posted on 2014-02-04
6
305 Views
Last Modified: 2014-02-11
Hi,

I tried below example.

http://www.avajava.com/tutorials/lessons/how-do-i-create-a-thread-in-java.html

When i run the example I  see output in irregular order.

Method 1: extending java.lang.Thread
Method 2: implementing java.lang.Runnable
ThreadSubclass is running
RunnableJob is running

How to fix this issue.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 3
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 400 total points
ID: 39834091
What do you mean by irregular order?

Note:  when you start the two threads... there is no guarantee which thread's output will reach the console first.  They should be independent of one another.
0
 
LVL 20

Assisted Solution

by:Amitkumar Panchal
Amitkumar Panchal earned 100 total points
ID: 39837854
Well, I guess the order of last three lines in the output will not be fixed. If you are considering it as an irregular order then it is an expected behaviour in multithreading program.

The reason behind is thread scheduling and it is driven by OS. OS decides which thread to run and put it back to runnable based on no of threads in the memory and its priority.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39843291
The reason behind is thread scheduling and it is driven by OS. OS decides which thread to run and put it back to runnable based on no of threads in the memory and its priority.

Which memory?
OS memory or java memory or laptop hard drive memory. please advise
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 19

Expert Comment

by:Ken Butters
ID: 39843686
Which memory?
OS memory or java memory or laptop hard drive memory. please advise

the handling of threads is done by the JVM. (Java Virtual Machine).

The JVM implementation can be different on different machines.

So the memory will be whatever memory is allocated to the JVM to run the java application.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39851196
So the memory will be whatever memory is allocated to the JVM to run the java application.

How and who allocate memory to JVM. I never remember allocating memory to JVM on my windows laptop. What is the maximum memory i can allocate to JVM.


I see my windowns 7, 64 bit laptop has installed memory(RAM) of 8 GB. How and where to assign memory to jvm. By default how much memory is allocated to JVM. Please advise
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39851256
Is your question about creating threads and the order that output from threads appear... or is it how to set JVM arguments?

Have you had your question answered about threads?

Sounds like you are moving on to another question about JVM parameters.

If your question has not been answered to your satisfaction, then you should ask for a clarification on the answer.

If your original question has been answered, you should allocate the points to the volunteer who was spent time answering your original question fully.  

Please feel free to open additional questions on additional topics as needed.

The order of the output from threads has absolutely NO impact due to the amount of memory allocated to the JVM.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now