Windows Azure Worker Role, what to do with the main thread? - multithreading

So we're setting up a worker role with windows azure and it's running at a very high cpu utilization. I think it has something to do with this section, but I'm not sure what to do about it. Each individual thread that gets started has its own sleep, but the main thread just runs in a while loop. Shouldn't there be a sleep in there or something?
public class WorkerRole : RoleEntryPoint
{
private List<ProcessBase> backgroundProcesses = new List<ProcessBase>();
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning);
}
}
while (true) { }
}
How about something like this, would this be appropriate?
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
List<Task> TaskList = new List<Task>();
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
TaskList.Add(Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning));
}
}
Task.WaitAll(TaskList.ToArray());
//while (true) { }
}

Your change looks good to me. Sometimes I use Thread.Sleep(Timeout.Infinite).
Have you tested it? Does it reduce the CPU usage? It could be that the tasks themselves actually consume a lot of CPU. We don't know for sure yet that the while loop is the culprit.

The while loop is probably causing your high CPU. It's basically an infinite busy-wait. Your second code sample should work fine, as long as the Tasks you're waiting on never exit. In my personal opinion the best solution is the one outlined in my answer here. If you don't like that, a simpler solution would be to add a Sleep() inside the loop. eg:
while(true){
Thread.Sleep(10000);
}

while loop with empty body will fully load the CPU core to which the thread is dispatched. That's bad idea - you burn CPU time for no good.
A better solution is to insert a Thread.Sleep() with a period ranging anywhere from 100 milliseconds to infinity - it won't matter much.
while( true ) {
Thread.Sleep( /*anything > 100 */ );
}
Once you've got rid of the empty loop body you're unlikely to do any better than that - whatever you do in your loop the thread will be terminated anyway when the instance is stopped.

Just deployed this to production this morning after testing it last night on staging. Seems to be working great. CPU usage went down to .03% average for the background process down from 99.5% ...
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
List<Task> TaskList = new List<Task>();
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
TaskList.Add(Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning));
}
}
Task.WaitAll(TaskList.ToArray());
//while (true) { }
}

Related

Thread thread = new Thread(() -> { /code } and Executorservice.submit(thread). How to write junit on this using powermock

Please help me write Junit for this piece of code using Mockito /Powermock, Finding it difficult due to lamda expression and executor service.
public class myClass {
ExecutorService executorService;
public void testMethod(String a){
Thread thread = new Thread(() -> {
//logic
a= testDAo.getStatus();
while (true) {
if (Thread.interrupted()) {
break;
}
if (a() != "done" || a() != "fail") {
Thread.yield();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
} else {
break;
}
}
}
Future task = executorService.submit(thread);
while (!task.isDone()) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
}
Various things here:
first of all: for testing executors and parallel execution, using a same thread executor can be extremely helpful (because it takes out the parallel aspect)
you have difficulties writing a unit test - because your production code is way too complicated.
Thus the real answer is: step back and improve your production code. Why again are you pushing a thread into an executor service?
The executor service is already doing things on a thread pool (at least that is how you normally use them). So you push a thread into a thread pool, and then you have code that waits "two" times (first within that thread, and then outside on the future). That just adds a ton of complexity for small gain.
Long story short:
I would get rid of that "inner thread" - just have the executor task wait until the result becomes available
Then: if lambda's give you trouble - then don't use them. Just create a named small class that implements that code. And then you can write unit tests for that small class. In other words: don't create huge "units" that do 5 different things. The essence of a good unit is to one thing (single responsibility principle!). And as soon as you follow that idea testing becomes much easier, too.

ThreadPoolExecutor is not executing concurrently?

This is for my academic purpose only. Are the tasks that we add to the executor service are really executing in parallel. Well this is my example that raised this question
Runnable Class
public Tasks implement Runnable{
int taskCount;
public Tasks(int count){
this.taskCount = count;
}
public void run(){
System.out.println("In Task :"+taskcount +" run method");
}
}
Main Class
Class MyTest {
public static void main(String args[]){
ExecutorService service = Executors.newFixedThreadPool(10);
for(inti=0;i<10;i++){
Tasks taskObj = new Tasks(i);
service.submit(taskObj);
}
service.shutdown();
}
}
As soon as i submit a taskObj to the executor, the taskObj run() is invoked.
What if i have to something like this,
Add all the taskObj to the executor , the run() must not get invoked
Execute all the task objects at one shot. All the taskobj run() must be executed in parallel/concurrently
Please let me know
Thanks...V
If I understood you right, one way to solve this would be to use thread barriers. This might sound strange, but is actually implemented quite easy. You just take a variable (lets name it traffic-light) and make every thread loop on it. If you started enough threads (starting a new thread might consume some time) you just change it to green and all your threads will start execution at the same time.
For academic purposes we used to take an atomic-integer as counter (initialized with 0) and started n threads. The task of each threads was to increase the counter and then loop on it until it reached n. Like this you'll have all threads as parallel as possible.
If you still want to go with a thread pool system, you might have to implement your own thread system, where threads can wait upon a signal prior to grabbing work.
good luck

How to run a function after a specific time

I want to as if there is any way to execute a function after a specific time in windows phone 7.? For instance, see this code in android:
mRunnable=new Runnable()
{
#Override
public void run()
{
// some work done
}
now another function
public void otherfunction()
{
mHandler.postDelayed(mRunnable,15*1000);
}
Now the work done in upper code will be executed after 15 seconds of execution of otherfunction().
And I want to know is this possible in any way in windows phone 7 also.?
Thanx to all in advance..
Although you can use the Reactive Extensions if you want, there's really no need. You can do this with a Timer:
// at class scope
private System.Threading.Timer myTimer = null;
void SomeMethod()
{
// Creates a one-shot timer that will fire after 15 seconds.
// The last parameter (-1 milliseconds) means that the timer won't fire again.
// The Run method will be executed when the timer fires.
myTimer = new Timer(() =>
{
Run();
}, null, TimeSpan.FromSeconds(15), TimeSpan.FromMilliseconds(-1));
}
Note that the Run method is executed on a thread pool thread. If you need to modify the UI, you'll have to use the Dispatcher.
This method is preferred over creating a thread that does nothing but wait. A timer uses very few system resources. Only when the timer fires is a thread created. A sleeping thread, on the other hand, takes up considerably more system resources.
You can do that by using threads:
var thread = new Thread(() =>
{
Thread.Sleep(15 * 1000);
Run();
});
thread.Start();
This way, the Run method wil be executed 15 seconds later.
No need for creating threads. This can be done much more easier using Reactive Extensions (reference Microsoft.Phone.Reactive):
Observable.Timer(TimeSpan.FromSeconds(15)).Subscribe(_=>{
//code to be executed after two seconds
});
Beware that the code will not be executed on the UI thread so you may need to use the Dispatcher.

Java-ME Application in Freeze Mode

I am developing a Java-ME Based Mobile Application. Now My Requirements are like whenever I am updating one of my RMS, I want my application to be stay in a Freeze kind of mode; which means no other action like clicking button or anything else should happen. My Method is already "Synchronized".
Kindly guide me regarding this question.
Thanks.
The best way to handle this is to "serialize" your tasks. You can do this with a message queue - a class that maintains a Vector of message objects (tasks) and runs code based on each message. The queue runs on a thread that processes each task (message) in series. You create a simple message class for the different tasks - read RMS etc. A message can be an Integer if you like that wraps a number. The operation of adding and retrieving messages is synchronized but the code than does the tasks is not and runs on a simple switch block. The benefit of serializing your tasks is you don't have to worry about concurrency. Here is some of the essential code from a class I use to do this.
class MessageQueue implements Runnable{
Vector messages;
Thread myThread;
volatile boolean stop;
public void start() {
stop=false;
myThread=new Thread(this);
myThread.start();
}
// add message to queue - this is public
public synchronized void addMessage(Message m) {
messages.addElement(m);
if(stop) {
start();
} else {
// wake the thread
notify();
}
}
// get next message from queue - used by this thread
private synchronized Message nextMessage() {
if(stop) return null;
if(messages.isEmpty()) {
return null;
} else {
Message m=(Message)messages.firstElement();
messages.removeElementAt(0);
return m;
}
}
public void run() {
while (!stop) {
// make thread wait for messages
if (messages.size() == 0) {
synchronized (this) {
try {
wait();
} catch (Exception e) {
}
}
}
if (stop) {
// catch a call to quit
return;
}
processMessage();
}
}
}
// all the tasks are in here
private void processMessage() {
Message m = nextMessage();
switch (m.getType()) {
case Message.TASK1:
// do stuff
break;
case Message.TASK2:
// do other stuff
break;
case Message.TASK3:
// do other other stuff
break;
default: //handle bad message
}
}
}
What you are asking is very code depended. Usually when you want to make some synchronic actions you just write them one after the other. in java it's more complected, since sometimes you "ask" the system to do something (like repaint() method). But since the RMS read/write operations are very quick (few millisecond) i don't see any need in freesing.
Could you please provide some more information about the need (time for RMS to respond)? does your code runs on system thread (main thread) or your own thread?
I want my application to be stay in a Freeze kind of mode; which means no other action like clicking button or anything else should happen.
First of all I would strongly advise against real freezing of UI - this could make a suicidal user experience for your application.
If you ever happened to sit in front of computer frozen because of some programming bug, you may understand why approach like this is strongly discouraged. As they describe it in MIDP threading tutorial, "user interface freezes, the device appears to be dead, and the user becomes frustrated..."
This tutorial by the way also suggests possibly the simplest solution for problems like you describe: displaying a wait screen. If you don't really have reasons to avoid this solution, just do as tutorial suggests.
To be on a safe side, consider serializing tasks as suggested in another answer. This will ensure that when RMS update starts, there are no other tasks pending.

Fire off multiple synchronous threads

I'm not sure if this is a silly question as I don't know much about threads, but is it possible to fire off multiple synchronous threads at the same time, and wait for all to complete before acting? If it is how do you do it?
Certainly the simplest way is to use .NET 4.0 's Task Parallel Library (TPL).
e.g.
Parallel.For(0, 10, x =>
// Do this in Parallel.
System.Diagnostics.Debug.WriteLine(x)
);
see: http://msdn.microsoft.com/en-us/concurrency/bb964701
ut is it possible to fire off multiple synchronous threads at the same time, and wait for all to complete before acting?
"synchronous threads" is an oxymoron, they don't exist.
Of course you can start multiple threads and wait for them to complete (Thread.Join(otherThread))
If it is how do you do it?
Very rarely. Always use as few threads as possible. They are expensive.
Make sure you know about the ThreadPool and (Fx4) the Tasks library, TPL
You can use Parallel.Invoke.
This will execute the supplied actions in parallel and return when all are finished.
You can't really do anything at the same time, let alone fire threads :) (you can fire them rapidly one after the other though, although it is possible that a thread will start before the last one is fired).
As for waiting for them all before continuing, you can use the Join method, which waits for a thread to end before continuing.
Generally you'd do with the construct like below,
public class MultipleThreqadTest
{
private readonly Thread[] threads;
private readonly object locker;
private int finishCounter;
private readonly AutoResetEvent waitEvent;
public MultipleThreqadTest()
{
threads=new Thread[10];
for(int i=0;i<0;i++)
threads[i]=new Thread(DoWork);
finishCounter = threads.Length;
waitEvent=new AutoResetEvent(false);
}
public void StartAll()
{
foreach (var thread in threads)
{
thread.Start();
}
//now wait for all worker threads to complete
waitEvent.WaitOne();
}
private void DoWork()
{
//Do Some Actual work here, you may need to lock this in case you are workin on some shared resource
//lock(locker)
//{
//}
//Check if all worker thread complets
if(Interlocked.Decrement(ref finishCounter)==0)
{
this.waitEvent.Set();
}
}
}

Resources