Threading – run another part of the program in a separate process

Threading in Java is very similar to the threading in c#, just like the example that I did before (threading in c sharp, running different-parts-of-the-program-at-once. What threading does is to allow for a part of your program to be run from within another process, this process runs normally independently of the main program (you can have links if you wanted to so some memory references).

The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, e.g. 10 milliseconds, and then leave the CPU execution stage whilst another process will enter there CPU execution stage (this is the part where the program is actually doing something).

So lets say that you want to spawn off another process that will talk to a server to get information back e.g. a tab browsing on a modern web browser, whilst you look at another part of the main program e.g another tab, and communicate with that one e.g. type in your login details etc.

So let start with a example of how to create a thread-able program. Here my class basicThread extends the class Thread, which encapsulate the thread-able interface.

public class basicThread extends Thread {

this thread-able interface basically only wants to have the method “run” implemented”, since that is what is called when you start the new thread.

	public void run()
	{
		System.out.println("Before my sleep");
		for (int i =0; i < 10; i++)
		{....

and then in your main program you just need to create a basicThread class and call the start method (from the Thread class that basicThread class inherited from)

		// create the thread, a thread is independent of the main program
		basicThread me = new basicThread();
		// then start it. this calls the run method
		me.start();

and that is it, it will spawn off another process that will run separately in time from the main process.

Here is the full source code, I have also included a file zip that has the source code and also the class files attached, save this as callThread.java

public class callThread {
	public static void main(String[] args) {
		// create the thread, a thread is independent of the main program
		basicThread me = new basicThread();
		// then start it. this calls the run method
		me.start();

		// so if the basic thread had that pause it in..
		System.out.println("main thread here!!");
		for (int j =0; j < 5; j++)
		{		
			System.out.println("Mainthread about to sleep at number : "+j);
			// need to pause the main thread
			try{
			  Thread.currentThread().sleep(100);//sleep for 1000 ms
			}	catch(Exception ie){
			}
		}
		System.out.println("End of the main thread");
		
	}

}

save this as basicThread.java

public class basicThread extends Thread {
	public void run()
	{
		System.out.println("Before my sleep");
		for (int i =0; i < 10; i++)
		{
			System.out.println("basicThread sleeping at number : "+i);
			try {
				sleep(45);
			} catch (InterruptedException e)
			{
				System.out.println("could not sleep long enought there was a error!!");
			}
			
		}
		System.out.println("after the sleep :)");
	}
}

here is the output, as you can see the mainthread and the basicthread outputs interrupt each other just like any other process does within a CPU.

main thread here!!
Before my sleep
Mainthread about to sleep at number : 0
basicThread sleeping at number : 0
basicThread sleeping at number : 1
basicThread sleeping at number : 2
Mainthread about to sleep at number : 1
basicThread sleeping at number : 3
basicThread sleeping at number : 4
Mainthread about to sleep at number : 2
basicThread sleeping at number : 5
basicThread sleeping at number : 6
Mainthread about to sleep at number : 3
basicThread sleeping at number : 7
basicThread sleeping at number : 8
Mainthread about to sleep at number : 4
basicThread sleeping at number : 9
after the sleep :)
End of the main thread

Threading – running different parts of the program at “once”

A thread is when you “spawn” off another process (a process is like another program running within itself).

The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, e.g. 10 milliseconds, and then leave the CPU execution stage whilst another process will enter there CPU execution stage (this is the part where the program is actually doing something).

To start with lets first create a method that is run within new thread, here we are just going to output a message and sleep for a bit, loop this over for 10 times.

// the method to create as a threadable method
public static void RunThisMethod()
{
	for (int i =0; i < 10; i++)
	{
		Console.WriteLine("RunThisMethod number : "+i.ToString() + " Sleep for 45");
		Thread.Sleep(45);
	}
}

Now lets create a thread, since a Thread has to be a delegated method but with .net 2 and above the .net environment will pick the correct delegate for you, so this means that you can just pass in the method name to the Thread class and that is it

	Thread theThread = new Thread(RunThisMethod);

To start the new Thread, it is as easy as start

	theThread.Start();

Here is the full source code, for demoing how the main and the runMyMethod flip between each other (e.g. each process has time in the processor(s) to run)

using System;
using System.Threading;

namespace myThread
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			// initialize the RunThisMethod as a thread
			Thread theThread = new Thread(RunThisMethod);
			theThread.Start();
			
			for (int j = 0; j < 5; j++)
			{
				Console.WriteLine("Main method number : "+j.ToString()+" Sleep for 100");
				Thread.Sleep(100);
			}
		}
		
		// the method to create as a threadable method
		public static void RunThisMethod()
		{
			for (int i =0; i < 10; i++)
			{
				Console.WriteLine("RunThisMethod number : "+i.ToString() + " Sleep for 45");
				Thread.Sleep(45);
			}
		}
	}
}

and here would be the output

Main method number : 0 Sleep for 100
RunThisMethod number : 0 Sleep for 45
RunThisMethod number : 1 Sleep for 45
RunThisMethod number : 2 Sleep for 45
Main method number : 1 Sleep for 100
RunThisMethod number : 3 Sleep for 45
RunThisMethod number : 4 Sleep for 45
Main method number : 2 Sleep for 100
RunThisMethod number : 5 Sleep for 45
RunThisMethod number : 6 Sleep for 45
Main method number : 3 Sleep for 100
RunThisMethod number : 7 Sleep for 45
RunThisMethod number : 8 Sleep for 45
Main method number : 4 Sleep for 100
RunThisMethod number : 9 Sleep for 45