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

One thought on “Threading – run another part of the program in a separate process”

  1. Thread.currentThread().sleep(100);
    should be written as
    Thread.sleep(100);
    because sleep() is a static method

Leave a Reply

Your email address will not be published. Required fields are marked *