Need explonation of this question about threads!
Hi to everyone, actually i wanna know why we have to use Threads. What is purpose of using threads since we can output the same result just with writting this code in main class:
Code Java:
System.out.print("thread #1: ");
for(int i=0; i<10; i++){
System.out.print(i+" ");
}
System.out.println();
System.out.print("thread #2: ");
for (int i = 10; i < 20; i++) {
System.out.print(i + " ");
}
---------------------------------------------------------------------------------------------
Code Java:
public class HelloRunnable implements Runnable {
//run method for HelloRunnable i.e. for the thread #1
public void run(){
System.out.print("thread #1: ");
for(int i=0; i<10; i++){
System.out.print(i+" ");
}
System.out.println();
}
Code Java:
public class HelloRunnable2 implements Runnable {
// run method for HelloRunnable2 i.e. for the thread #2
public void run() {
System.out.print("thread #2: ");
for (int i = 10; i < 20; i++) {
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
(new Thread(new HelloRunnable())).start();
(new Thread(new HelloRunnable2())).start();
}
}
what is actually the difference doing this with thread and doing without threads since the result is the same?
Anyone who can explain it ?
Re: Need explonation of this question about threads!
Quote:
Originally Posted by
lulzimfazlija
what is actually the difference doing this with thread and doing without threads since the result is the same?
Anyone who can explain it ?
Basically, this is a bad example of Threading. Instead: what happens if each Thread prints out a trillion numbers? What happens if you put a Thread.sleep(100) after printing out each number?
I wouldn't really expect you to get the same output each time, even with your small example. I recommend providing an SSCCE that we can run that demonstrates what you're doing.
Re: Need explonation of this question about threads!
Quote:
...actually i wanna know why we have to use Threads. What is purpose of using threads...
Agreed, a bit of a bad example. Threads allow you to perform work in parallel. Doing so allows you to essentially do 2+ things at once (actually its a bit more complex than that but from a layman's standpoint that's what they do). This can reduce the time it takes to complete a task (can divide the work up), prevent locking up a GUI, or perform tasks which need to be done seemingly the same time
Re: Need explonation of this question about threads!
So is there any practical example that anyone can show us or just theory , bc im alredy tired from theory.
Re: Need explonation of this question about threads!
I gave you a few ways to make your example more practical, and the Concurrency tutorials contain numerous examples: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
Re: Need explonation of this question about threads!
Quote:
Originally Posted by
lulzimfazlija
So is there any practical example that anyone can show us or just theory , bc im alredy tired from theory.
One common example is playing music (particularly in games).
If you were only using one thread to do this, then the game wouldn't be able to continue until the music has finished playing. However, if you use two threads then you can update the game in one thread while the other one is playing the music.
Re: Need explonation of this question about threads!
Anything which can be done asynchronously (at the same time, independent of each other) can benefit from multi-threading.
In your example, this isn't the case (I'm assuming you want the numbers printed out in order) because all the numbers in HelloRunnable have to be printed out before you want the numbers in HelloRunnable2 to be printed out, which is a synchronous operation(HelloRunnable2 must wait for HelloRunnable to finish to get the correct results).
There are many examples of things which can be run asynchronously. Generally, they fall into these two broad categories:
1. You're looking to speed up something which is computationally expensive and can be split up into asynchronous tasks, either partly or in whole. Note that you'll only get any appreciable speed-up if you actually have multiple cores on your computer.
2. You're looking to improve the response of different asynchronous components (such as a GUI interface which can still receive user input while data processing happens on another thread to prevent either side from hanging). This can be beneficial on single core systems as well as multi core systems because the OS can switch between the tasks automatically for you, therefore if you're computation takes a long time, some CPU clocks are still being diverted to your GUI control allowing the user to still use it at the same time.