Differences

This shows you the differences between two versions of the page.

Link to this comparison view

javathreadexample [2014/10/26 01:52] (current)
Line 1: Line 1:
 +===== Java Thread Example with calling program =====
 +
 +  * Thread
 +
 +<code java>
 +// 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);
 + }
 + }
 +}
 +
 +</​code>​
 +
 +  * Calling program
 +
 +<code java>
 +// 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();​
 + }
 +
 +}
 +
 +</​code>​
 +
 +----
 +
 +  * [[javainfo|Back to Java]]
  
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