Java Thread Example with calling program

  • Thread
// Threaded.java
//
 
public class Threaded extends Thread
{
	private String identifier = "Threaded";
	private int iterations = 5;
 
	// Constructor
	Threaded()
	{
	}
 
	public void setIterations(int n)
	{
		this.iterations = n;
	}
 
	public int getIterations()
	{
		return this.iterations;
	}
 
	// getIdentifier()
	public String getIdentifier()
	{
		return this.identifier;
	}
 
	// setIdentifier()
	public void setIdentifier(String n)
	{
		this.identifier = n;
	}
 
	// run()
	public void run()
	{
		for (int i = 0; i<this.iterations; i++)
		{
			System.out.println("Thread "+this.getIdentifier()+": value = "+i);
		}
	}
}
  • Calling program
// Threaded.java
//
 
public class ThreadTest
{
	public static void main(String args[])
	{
		Threaded a = new Threaded();
		Threaded b = new Threaded();
		a.setIdentifier("A");
		a.setIterations(5000);
		b.setIdentifier("B");
		b.setIterations(5000);
		a.start();
		b.start();
	}
 
}

javathreadexample.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0