Skip to content

Instantly share code, notes, and snippets.

@Hackin7
Created October 27, 2018 23:55
Show Gist options
  • Save Hackin7/ab7b3b824c5b10c51811cf8e3a2b0888 to your computer and use it in GitHub Desktop.
Save Hackin7/ab7b3b824c5b10c51811cf8e3a2b0888 to your computer and use it in GitHub Desktop.
A Java multithreading demo about running 3 threads simultaneously
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