|
First things Second, I mean first. What is MultiThreading? Multithreading is a lot like cutting your processor in half and have one half do something while the other half does another thing. It doesnt work exactly like that, but it is similar. It really switches between tasks very quickly, so it isnt truly running more than one thing at once. Multithreading allows you to say, for example, take user input while printing out random numbers AT THE SAME TIME. For this tutorial you should at least know the basics of java, and Inner Classes (Isnt much to know except they are inside another class (And they can access all of its private data)). Lets get started...
First, we need to make a class which will be what the thread will be performing. It has to implements the runnable interface, meaning you need a method called run.
| CODE | public class MultiThreading { public class ThreadJob implements Runnable { public void run() { //Stuff that the thread will execute goes here } } }
|
Now, that wouldnt actually do anything, but its a start. We need to create the thread now.
| CODE | public class MultiThreading { public static void main(String[] args) { new MultiThreading().start(); } public void start() { Runnable r = new ThreadJob(); Thread t = new Thread(r); t.start(); } public class ThreadJob implements Runnable { public void run() { //Stuff that the thread will execute goes here } } }
|
Now that makes a new runnable(A job for a thread) which is ThreadJob. Then it passes the job onto the Thread. The thread then starts and run() executes.
Here is an example showing which threads run when. Every time you run it you will get a different result.
| CODE | public class MultiThreading { public static void main(String[] args) { new MultiThreading().start(); } public void start() { Runnable r = new ThreadJob(); Thread t = new Thread(r); t.start();
Runnable r2 = new ThreadJob2(); Thread t2 = new Thread(r2); t2.start(); } public class ThreadJob implements Runnable { public void run() { for(int i = 0; i < 10; i++) { System.out.println("Thread 1 running"); } } }
public class ThreadJob2 implements Runnable { public void run() { for(int i = 0; i < 10; i++) { System.out.println("Thread 2 running"); } } } }
|
Now, there can be some disasters when it comes to threads. Suppose there is a bank account, and two people (Threads) share. Look at this example code:
| CODE | public void withdraw(double amount) { if(amount < currentAmountInBankAccount) { currentAmountInBankAccount -= amount; } }
|
Now, suppose Thread one enters the if statement, then thread two take over. Thread two enter the if statement and takes out the money. Thread one takes over again and takes out money. OH NO! What if together they took out more than they had?! Little cheats... Anyway, to prevent two threads from accessing a method at the same time, use the synchronized keyword.
| CODE | public synchronized void withdraw(double amount) { if(amount < currentAmountInBankAccount) { currentAmountInBankAccount -= amount; } }
|
Or, if only part of it is synchronized, do this:
| CODE | public void withdraw(double amount) { synchronized(this) { if(amount < currentAmountInBankAccount) { currentAmountInBankAccount -= amount; } } }
|
Aha, no more little cheaters now! Hopefully you now know how to use threads, what they are used for, and how to make sure your code is thread safe.
|