Category Archives: Multithreading

.NET Channels

This post is based upon a Channel9 video with Stephen Toub, I recommend you go and watch it (see references) if you’re interested in threadsafe producer/consumer mechanisms.

This is the namespace you want to be looking at

using System.Threading.Channels;

There are two types of channel, bounded and unbounded.

Bounded channels have a fixed size. In this example the queue within the channel will only allow a maximum of 10 items, meaning if we try to write more than the 10 items, the write method would block until an item is removed/read from the queue

var c = Channel.CreateBounded<int>(10);

So from this, we can tell that unbounded means the queue within the channel in unlimited in size. We create an unbounded channel like this

var c = Channel.CreateUnbounded<int>();

When we want to add items to the channel we use code such as the following, obviously if the queue has reached a limit (bounded) then this will block

c.Writer.TryWrite(123);

To read a value from the channel (if one exists) we use

await c.Reader.ReadAsync()

References

Working with Channels in .NET
An Introduction to System.Threading.Channels

ExecutorService based multi-threading in Java

A threading feature within Java is the ExecutorService. This allows us to, in essence, create our own threadpools. This is useful because it allows us to create a limited number of threads that our code may run on, ensuring we do not end up with thread starvation or the number of threads getting out of hand.

By writing our code to use the ExecutorService we can also limit our methods to only use a single thread if we want.

Here’s a simple example of using the ExectorService

public static void main(String[] args) throws 
   InterruptedException, 
ExecutionException {

   System.out.println("Main Thread - " + Thread.currentThread().getId());

   //ExecutorService executorService = Executors.newSingleThreadExecutor();
   ExecutorService executorService = 
      Executors.newFixedThreadPool(
         Runtime.getRuntime().availableProcessors());

   Future<?> f1 = executorService.submit(() 
      -> outputInformation(1));
   Future<?> f2 = executorService.submit(() 
      -> outputInformation(2));
   Future<?> f3 = executorService.submit(() 
      -> outputInformation(3));

   // the equivalent of waiting on the threads
   f1.get();
   f2.get();
   f3.get();

   executorService.shutdown();
}

private static void outputInformation(int id) {
   try {
      // some arbitrary time wasting
      Thread.sleep(2000);
      System.out.println(id + " - " + Thread.currentThread().getId());
   }catch (Exception e) {
   }
}

In the code above, we are creating our own threadpool (using Executors.newFixedThreadPool) with the size based upon the number of available processors. Hence in this example (assuming you have more than 1 processor on your machine) the resulting output is indeterminant, i.e. we might see 2 followed by 1, followed by 3 and all on different threads.

By simply changing to use Executors.newSingleThreadExecutor() the output will be in the correct order as only a single thread is used for each call.

The ExecutorService is a useful abstraction for containing and controlling multiple threads.