Created
October 27, 2018 23:55
-
-
Save Hackin7/ab7b3b824c5b10c51811cf8e3a2b0888 to your computer and use it in GitHub Desktop.
A Java multithreading demo about running 3 threads simultaneously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Running implements Runnable { | |
private Thread t; | |
private String name; | |
Running( String threadName) { | |
name = threadName; | |
} | |
public void run() { | |
//String name = "A"; | |
int sum = 0; | |
for(int x = 0; x < 9; x++){ | |
sum += x; | |
System.out.println("Thread : "+name+" - value : "+Integer.toString(x)); | |
} | |
System.out.println("Thread : "+name+" - sum : "+Integer.toString(sum)); | |
} | |
public void start () { | |
if (t == null) { | |
t = new Thread (this, name); | |
t.start (); | |
} | |
} | |
} | |
class Main { | |
public static void main(String[] args) { | |
Running A = new Running("A"); | |
Running B = new Running("B"); | |
Running C = new Running("C"); | |
A.start(); | |
B.start(); | |
C.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment