INDEX ABOUT & TOS PORTAL HELP SEARCH MEMBERS CALENDAR

Create a free forum in seconds.
InvisionFree - Free Forum Hosting
Welcome to beta name Azure Echo v 0.8. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Name:   Password:


 

 [Java] Multithreading
Fasga
Posted: Jun 15 2006, 01:05 AM


Newbismal Newb


Group: Members
Posts: 6
Member No.: 22
Joined: 13-June 06



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.
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
InvisionFree - Free Forum Hosting
Join the millions that use us for their forum communities. Create your own forum today.

Topic Options



Azure Echo Shoutbox
..::4ZUR3_3C|-|O::..
Hosted for free by InvisionFree* (Terms of Use: Updated 2/10/2010) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.1587 seconds | Archive
-----------
© 2006-2007 Azure Echo, LLC. All content and images are property of Azure Echo, LLC. unless otherwise specified.
Free Web Counter