I want to clear my concept on thread scheduling. I have asked this question before but due to no response i am trying to ask it in convinient manner My system has 4 processors when I run single CPU-bound task on a thread it completes in around 30ms. When I run 2 CPU-bound tasks on two threads they complete in around 70ms.
Why do 2 tasks running on two threads take twice the time?
CPU-bound task:
public class CpuBoundJob3 implements Runnable {
//long t1,t2;
public void run() {
long t1=System.nanoTime();
String s = "";
String name="faisalbahadur";
for(int i=0;i<10000;i++)//6000 for 15ms 10000 for 35ms 12000 for 50ms
{
int n=(int)Math.random()*13;
s+=name.valueOf(n);
//s+="*";
}
long t2=System.nanoTime();
System.out.println("Service Time(ms)="+((double)(t2-t1)/1000000));
}
}
Thread to run a task:
public class TaskRunner extends Thread {
CpuBoundJob3 job=new CpuBoundJob3();
public void run(){
job.run();
}
}
Main class:
public class Test2 {
int numberOfThreads=100;//for JIT Warmup
public Test2(){
for(int i=1;i<=numberOfThreads;i++){for JIT Warmup
TaskRunner t=new TaskRunner();
t.start();
}
try{
Thread.sleep(5000);// wait a little bit
}catch(Exception e){}
System.out.println("Warmed up completed! now start benchmarking");
System.out.println("First run single thread at a time");
//run only one thread at a time
TaskRunner t1=new TaskRunner();
t1.start();
try{//wait for the thread to complete
Thread.sleep(500);
}catch(Exception e){}
//Now run 2 threads simultanously at a time
System.out.println("Now run 2 thread at a time");
for(int i=1;i<=2;i++){//run 2 thread at a time
TaskRunner t2=new TaskRunner();
t2.start();
}
}
public static void main(String[] args) {
new Test2();
}
}
Another phenomenon that sometimes happens is "false sharing". This happens when one thread reads from the same cache line (typically 64 consecutive bytes, depending on the system) that another thread is writing. This is bad for the cache, and this slows down memory access.
When I wrote my first multithreaded program this happened to me as follows: I assigned one thread to operate on the even places of an array, and another thread to operate on the odd places. This is perfectly thread-safe, but very slow, because when thread 1 writes at the array in place #n, thread 2 is reading from place #(n+1), which is the same cache-line as place #n, so the cache keeps getting invalidated.
See also https://en.wikipedia.org/wiki/False_sharing
Related
Consider the following code:
public class Test {
static boolean moreLoop = true;
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(() -> {
while (moreLoop) {
// more loop
}
System.out.println("loop exited");
});
Thread t2 = new Thread(() -> {
moreLoop = false;
});
t1.start();
Thread.sleep(1000); // so we have more confidence t1 runs first
t2.start();
}
}
After running the code, the "loop exited" statement never gets printed. I understand this is because thread 2 puts the updated boolean in the cache of the CPU in which it's running, so thread 1 can not see the change if it's running in a different CPU. If we make the moreLoop variable volatile then "loop exited" will soon be printed.
Here's the thing. If we leave moreLoop non-volatile and write Thread.currentThread().yield(); on the line after // more loop inside the while loop, we will see "loop exited" printed. Using Thread.sleep will achieve the same effect. Why is that? Does thread 1 sync with thread 2 when it is restored?
I ask this question because my company has a WebApp written in Java that never uses volatile on the shared hash maps and the code has been working for years. My guess is that because the HTTP threads are managed by tomcat, they might get scheduled out after finishing one HTTP request and before serving the next one, just like what I described in the above paragraph. Is this possibly the reason why the code works?
Edit: If we don't use either volatile or yiled/sleep, it seems thread 1 can still see the change made by thread 2 as long as the change by thread 2 is made before the while loop starts. For example, after running the following code
public class Test {
static boolean moreLoop = true;
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(() -> {
System.out.println("thread 1 started");
for (long i = 0; i < 10_000_000_000L; ++i); // takes around 3 seconds on my machine
System.out.println("thread 1 warmup finished");
while (moreLoop) {
// more loop
}
System.out.println("loop exited");
});
Thread t2 = new Thread(() -> {
System.out.println("thread 2 started");
moreLoop = false;
System.out.println("thread 2 finished");
});
t1.start();
Thread.sleep(1000); // so we have more confidence t1 runs first
t2.start();
}
}
it prints the following on my machine
thread 1 started
thread 2 started
thread 2 finished
thread 1 warmup finished
loop exited
We don't have any synchronization. Well, you may argue System.out.println does the synchronization. But after I removed all println statements except "loop exited", the code could still print "loop exited". How come thread 1 could see the change made by thread 2?
I would like to understand that does java actually run multiple threads in parallel in a multi core CPU, or there is context switching between threads and only one thread is active and others are waiting for their turn to run.
In other words, is there a possibility that 2 threads are running in parallel???
Because my Thread.currentThread() does not give me a array of threads, but only one thread which is running.
So what is the truth, does only one thread run at a time while others wait or multiple threads can run in parallel, if yes , then why my Thread.currentThread() method return only 1 thread object.
Edit : .....
I have created 2 classes to count numbers
1 class does it synchronously and the other one divides it into two halves and executes the two halves in 2 threads..(intel i5(4 CPUs), 8GB ram)
the code is as follows :
common class :
class Answer{
long ans = 0L;}
Multi Thread execution :
public class Sheet2 {
public static void main(String[] args) {
final Answer ans1 = new Answer();
final Answer ans2 = new Answer();
Thread t1 = new Thread(new Runnable() {
#Override
public void run() {
for(int i=0;i<=500000; i++) {
ans1.ans = ans1.ans + i;
}
}
});
Thread t2 = new Thread(new Runnable() {
#Override
public void run() {
for(int i=500001;i<=1000000; i++) {
ans2.ans = ans2.ans + i;
}
}
});
long l1 = System.currentTimeMillis();
try {
t1.start();t2.start();
t1.join();
t2.join();
long l2 = System.currentTimeMillis();
System.out.println("ans :" + (ans1.ans + ans2.ans) +" in "+(l2-l1) +" milliseconds");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Single Thread execution :
public class Sheet3 {
public static void main(String[] args) {
final Answer ans1 = new Answer();
long l1 = System.currentTimeMillis();
for(int i=0;i<=1000000; i++) {
ans1.ans = ans1.ans + i;
}
long l2 = System.currentTimeMillis();
System.out.println("ans :" + (ans1.ans ) +" in "+(l2-l1) +" milliseconds"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
My Single thread execution is faster than my multi threaded execution, I though the context switching was putting a overhead on the execution initially and hence the multithreaded execution output was slower, now I am having muti core CPU (4 CPU), but still single threaded execution is faster in this example..
Can you please explain the scenario here... is it because my other processes are eating up the other cores and hence my threads are not running in parallel and performing time slicing on the CPU ???
Kindly throw some light on this topic.
Thanks in advance.
Cheers.!!!
In short yes it does run on separate threads. You can test it by creating 100 threads and checking in your process explorer it will say 100 threads. Also you can do some computation in each thread and you will see your multicore processor go upto 100% usage.
Thread.currentThread gives you the current thread you are running from.
When you start your program you are running on the "main" thread.
As soon as you start a new thread
new Thread(myRunnable);
any code located in the myRunnable will run on the new thread while your current thread is is still on the main Thread.
If you check out the API http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html it gives allot more detailed description of the thread.
The actual threading mechanism can vary between CPU architectures. But the real problem is that you're misinterpreting the method name. Thread.currentThread() doesn't return the thread executing at the current moment in time; it returns the thread currently executing the method call, that is, itself.
Yes it does. Run any simple infinite loop on more than one threads and you'll see the cpu usage > 100% on a multi-core CPU.
Yes it does run threads concurrently .That is the very purpose of the multithreading concept .you may find the folowing discussion helpful :
Current Thread Method java
Not a complete answer here, just adding to what others already have said:
The Java Language Specification does not require that threads run in parallel, but it allows them to do so. What actually happens in any given Java Virtual Machine depends on how that JVM is implemented.
Any practical JVM will create one "native" (i.e., operating system) thread for each Java thread, and let the operating system take care of scheduling, locking, waiting, notifying,...
I'm trying to start a thread in a for-loop. This task should only wait for a second (Thread.sleep()), so every time the loop starts over again, a new thread is started and it should cause the code after the thread to wait until it is executed.
public void count()
{
for(int i = 29; i>=0; i--)
{
Thread t1;
t1 = new Thread(new TimerClass());
t1.start();
String s = String.valueOf(i);
jLabel6.setText(s);
System.out.println(s);
}
}
public class TimerClass implements Runnable{
#Override
public void run()
{
try{
Thread.sleep(1000);
System.out.println("Timer");
} catch(InterruptedException e)
{
}
}
}
As you can see, I implemented in both methods System.out.println() to check if they are actually executed. I get this:
29
28
27
26
...//25 - 3
2
1
0
Timer
Timer
Timer
//in all 29 times Timer
So it should be 29, Timer, 28, Timer and so on, but it isn't.
Does anyone know what's wrong with the code?
Thanks a lot.
Your main loop that is starting the thread is likely dominating the CPU, so it finishes doing its entire loop and only then do the threads get a chance to go.
In fact, given that all of your threads sleep for an entire second and you're only looping 29 times, you're guaranteed that your loop will finish (and print all of the numbers) before your threads do. Add a sleep to your main loop if you want the threads to print - remember, the main loop doesn't stop when you start a thread.
You can join a thread to the main thread so first your thread will finished then main thread
public void count()
{
for(int i = 29; i>=0; i--)
{
Thread t1;
t1 = new Thread(new TimerClass());
t1.start();
t1.join();
String s = String.valueOf(i);
jLabel6.setText(s);
System.out.println(s);
}
}
Here is my code for spawning 2 threads or one thread depends on arrayList size but in my case this threads are doing much more complex tasks then just waiting 1 sec
for (int i = 0; i < array.size(); i += 2) {
Thread t1 = null;
Thread t2 = null;
if (i < array.size() - 1 && array.size() > 1) {
t1 = new Thread(array.get(i));
t2 = new Thread(array.get(i + 1));
t1.start();
t2.start();
}
else {
t2 = new Thread(array.get(i));
t2.start();
}
if (t1 != null)
t1.join();
if (t2 != null)
t2.join();
}
In my code I populate arrayList with Objects that Implements Runnable interface.
Even if you sleep the thread for 1ms, your results would be the same. If you can manage the thread to sleep for the time less than it takes to print the results, your result could be as expected. Here is my code where I have put the time of 1 ms but yet the results are the same.
public class MultiThreading implements Runnable
{
public void run()
{
try
{
Thread.sleep(1);
System.out.println("Timer");
}
catch(Exception e)
{
}
}
public static void main(String [] args)
{
for(int i = 29; i>=0; i--)
{
Thread t1;
t1 = new Thread(new MultiThreading());
t1.start();
String s = String.valueOf(i);
System.out.println(s);
}
}
}
If you comment out the Thread.sleep(1) method, then your results are as you expected.
Delay is much enough to let the for loop in count() to finish before is can print 'timer' from thread.
What is happening is that the thread you started starts executing and immediately goes to sleep. In the meantime, your loop just keeps running. As the whole point of threads is that they run asynchronously, I don't really understand why you think your main loop should be waiting for it to finish sleeping. The thread has started running and is now running independently of the main loop.
If you want to wait for the thread you just started to finish (in which case, you might as well use a method), then use one of the synchronisation primitives, i.e. Thread.wait().
What you actually want to do is block your main thread while another thread is running. Please don't use Thread#sleep statements, as these are unreliable in order to "make your application work". What you want to use instead is Thread#join. See dharr his code for an example.
Also, it's better to use Executors and ExecutorServices when creating threads or running async tasks.
Threads are interesting. Think of a virtual thread as a physical thread. There are many threads on the clothes you're wearing, all working at the same time to hold your shirt together. In virtual terms what Thread.start() does is start a thread on a different strand WHILE the following code continues to execute, (i.e. Two Threads work simultaneously like 2 runners run next to each other). Consider putting a break point right after Thread.start(). You'll understand.
For your desired effect, just put a Thread.sleep() in the main loop. This will cause an output of
29
Timer
28
Timer
// etc.
Hope this helped.
Jarod.
Another analogy to the threads in a shirt:
Think of threads as coworkers to your main programm (which is a thread itself). If you start a thread, you hand some work to this coworker. This coworker goes back to his office to work on this task. You also continue to do your task.
This is why the numbers will appear before the first thread/coworker will output anythig. You finished your task (handing out work to other coworkers) before he finished his.
If you want to give out some work and then wait for it to be finished, use t1.join() as suggested by others. But if you do this, it is senseless to create new Threads, because you don't (seem) to want to process something in parallel (with many coworkers) but in a specific order - you can just du it yourself.
Practicing multi thread java examples ,here i am creating thread A in class A and thread B in class B .Now starting those two threads by creating objects .here i am placing the code
package com.sri.thread;
class A extends Thread
{
public void run()
{
System.out.println("Thread A");
for(int i=1;i<=5;i++)
{
System.out.println("From thread A i = " + i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Thread B");
for(int i=1;i<=5;i++)
{
System.out.println("From thread B i = " + i);
}
System.out.println("Exit from B");
}
}
public class Thread_Class
{
public static void main(String[] args)
{
new A().start(); //creating A class thread object and calling run method
new B().start(); //creating B class thread object and calling run method
System.out.println("End of main thread");
}
//- See more at: http://www.java2all.com/1/1/17/95/Technology/CORE-JAVA/Multithreading/Creating-Thread#sthash.mKjq1tCb.dpuf
}
i didn't understand the flow of execution ,tried by debugging but didn't get it.how the flow of execution is .Here i am placing the out put which confusing me.
Thread A
Thread B
End of main thread
From thread B i = 1
From thread B i = 2
From thread B i = 3
From thread B i = 4
From thread B i = 5
Exit from B
From thread A i = 1
From thread A i = 2
From thread A i = 3
From thread A i = 4
From thread A i = 5
Exit from A
Why does the loop in thread B finish before the loop in thread A is entered?
Unless you have multiple processors, the threads are sharing a processor on a time slice basis. The total time to execute one of your threads may be less than the time slice, so whichever thread gets dispatched first will complete before the other runs at all.
Try adding a short Thread.sleep call in the run methods.
There really is no guaranteed order of execution when executing multiple threads. The threads are independent of each other.
The link in the source code explains it:
Here you can see that both outputs are different though our program
code is same. It happens in thread program because they are running
concurrently on their own. Threads are running independently of one
another and each executes whenever it has a chance.
The threads are executing at the same time. One isn't executing inside the other. Each thread gets CPU time, does some work, and then waits while the CPUs are busy with other things. It could run differently each time depending on what else is happening on the computer.
If you expected the threads' output messages to be interlaced, they will be, to an extent, if you increase your loops to several thousands of iterations. Right now the code finishes so quickly it's hard to see that they really are running in parallel.
public abstract class Multithread implements Runnable{
static Thread t1 = new Thread(){
public synchronized void run(){
try {
for(;;){
System.out.println("java");
t1.sleep(300);
}
} catch (Exception e) {
System.out.println("Exception"+e);
}
}
};
static Thread t2 = new Thread(){
public synchronized void run(){
try{
for(;;){
System.out.println("world");
t2.sleep(300);
}
}catch(Exception e){
System.out.println("Exception"+e);
}
}
};
public static void main(String[] args) {
try{
t1.start();
t2.start();
}catch(Exception e){
System.out.println("Exception "+e);
}
}
#Override
public void run() {
System.out.println("running");
}
}
Excepted O/P:
java
world
java
world
java
world
.
.
.
.
observed
I tried using sleep() for the threads,they are getting overlapped at some point of time like this-
java
java
world
java
world
world
java
..
Expected
i need these two threads to run in parallel and it should not get overlapped,either of the thread can be started. Any idea?
Threads are not accurate in time. If they depend on each other, you have to manually synchronize, like : How to execute two threads Sequentially in a class
You can achieve this by having a counter, if the counter is 0 thread 1 executes, increments the counter and calls notyfyAll() on the counter, if the counter is 1 thread 1 calls wait() on the counter. And vice versa for thread 2.
When you execute two threads in parallel, each one of them is executed independently by the underlying operating system. The task scheduler can decide when to execute which thread. This can result in uneven time distribution like you are experiencing.
One solution for this is to use shared Semaphores to lock each thread until the other one has finished its task. Each thread would then take two semaphores, one for itself and one for the other thread. Both semaphores would start with one permit, so the first release() call doesn't block. The threads would then work like that (psudocode):
for (;;) {
otherThreadSemaphore.acquire(); // blocks until the other thread called release()
performWork();
ownThreadSemaphore.release(); // allows the other thread to perform another iteration
}
Another would be to move the infinite loop out of the threads. In each iteration of the loop you spawn both threads, then use thread.yield() to wait until both threads are finished and then the loop starts again creating two new threads. But note that creating threads can be an expensive operation, so you should only do this when the threads do considerable amount of work in each iteration so that this doesn't matter much.