Java Thread Example?

Can someone give an example program that explains Java Threads in a simple way? For example, let's say I have three streams t1 , t2 and t3 . I need code that demonstrates that threads execute at the same time, not sequentially.

+43
java multithreading
Mar 28 '10 at 4:53 on
source share
4 answers

Here is a simple example:

ThreadTest.java

 public class ThreadTest { public static void main(String [] args) { MyThread t1 = new MyThread(0, 3, 300); MyThread t2 = new MyThread(1, 3, 300); MyThread t3 = new MyThread(2, 3, 300); t1.start(); t2.start(); t3.start(); } } 

Mythread.java

 public class MyThread extends Thread { private int startIdx, nThreads, maxIdx; public MyThread(int s, int n, int m) { this.startIdx = s; this.nThreads = n; this.maxIdx = m; } @Override public void run() { for(int i = this.startIdx; i < this.maxIdx; i += this.nThreads) { System.out.println("[ID " + this.getId() + "] " + i); } } } 

And some conclusion:

 [ID 9] 1 [ID 10] 2 [ID 8] 0 [ID 10] 5 [ID 9] 4 [ID 10] 8 [ID 8] 3 [ID 10] 11 [ID 10] 14 [ID 10] 17 [ID 10] 20 [ID 10] 23 

Explanation. Each MyThread object tries to print numbers from 0 to 300, but they are only responsible for certain areas of this range. I decided to break it down by index, with each thread jumping forward in the number of threads. So, t1 has an index of 0, 3, 6, 9, etc.

Now, without I / O, trivial calculations like this might still look like threads being executed sequentially, so I just showed the first part of the output. On my computer, after this, the output stream with identifier 10 ends immediately, and then 9, then 8. If you put an expectation or profitability, you can see it better:

Mythread.java

 System.out.println("[ID " + this.getId() + "] " + i); Thread.yield(); 

And the conclusion:

 [ID 8] 0 [ID 9] 1 [ID 10] 2 [ID 8] 3 [ID 9] 4 [ID 8] 6 [ID 10] 5 [ID 9] 7 

Now you can see the execution of each thread, give up control at an early stage and the next execution.

+74
Mar 28 '10 at 5:20
source share

There is no guarantee that your threads are running at the same time, regardless of any trivial example. If your OS only gives the java process one processor to run, your java threads will still be scheduled for each temporary fragment in a loop. This means that none of the two will be performed simultaneously, but the work that they perform will alternate. You can use monitoring tools such as Java Visual VM (standard in the JDK) to monitor threads running in the Java process.

+7
Mar 28 '10 at 7:02
source share

A simple example:

 public class Test extends Thread { public synchronized void run() { for (int i = 0; i <= 10; i++) { System.out.println("i::"+i); } } public static void main(String[] args) { Test obj = new Test(); Thread t1 = new Thread(obj); Thread t2 = new Thread(obj); Thread t3 = new Thread(obj); t1.start(); t2.start(); t3.start(); } } 
+3
Feb 27 '13 at 16:24
source share

create a java application in which you define two streams, namely t1 and t2, stream t1 will generate random numbers 0 and 1 (simulate a coin toss). 0 means the head and one tail of the product. another thread t2 will do the same t1 and t2 will repeat this cycle 100 times, and finally, your application should determine how many times t1 guesses the number generated by t2, and then displays the count. for example, if the stream t1 guesses 20 out of 100 numbers, then the estimate t1 is 20/100 = 0.2, if t1 guesses about 100 numbers, then it gets the estimate 1, etc.

0
Mar 24 '13 at 16:41
source share



All Articles