Skip to content

Instantly share code, notes, and snippets.

@paperlefthand
Created February 16, 2020 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paperlefthand/5c3459958a8f6e6ea96d42cd4bee635e to your computer and use it in GitHub Desktop.
Save paperlefthand/5c3459958a8f6e6ea96d42cd4bee635e to your computer and use it in GitHub Desktop.
Javaでマルチスレッド
package com.example.multithread;
public class Main {
public static void main(String[] args) {
MultiThread mt1 = new MultiThread(1);
MultiThread mt2 = new MultiThread(2);
MultiThread mt3 = new MultiThread(3);
mt1.start();
mt2.start();
mt3.start();
}
}
class MultiThread extends Thread {
MultiThread(int tn){
this.threadNumber = tn;
}
private int threadNumber;
@Override
public void run() {
for (int i=1;i<4;i++) {
try {
Thread.sleep(1000);
System.out.println(
"Thread"+this.threadNumber+" counts "+i+" times."
);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// Output Sample
// Thread3 counts 1times.
// Thread2 counts 1times.
// Thread1 counts 1times.
// Thread1 counts 2times.
// Thread2 counts 2times.
// Thread3 counts 2times.
// Thread3 counts 3times.
// Thread1 counts 3times.
// Thread2 counts 3times.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment