Created
February 16, 2020 04:06
-
-
Save paperlefthand/5c3459958a8f6e6ea96d42cd4bee635e to your computer and use it in GitHub Desktop.
Javaでマルチスレッド
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
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