- スレッドは複数起動することができる
- スレッドを起動するにはThreadクラスを拡張し、start()メソッドを呼び出す
- スレッドの最初の処理として、run()メソッドを呼び出す
- スレッドを一時停止するには、sleep()メソッドを呼び出す
- スレッドの終了を待つには、join()メソッドを呼び出す
- 複数のスレッドが共有するフィールドなどを処理するときは、メソッドにsynchronizedを指定する
Last active
December 28, 2015 02:29
-
-
Save shigemk2/7427865 to your computer and use it in GitHub Desktop.
やさしいJava 15
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
// 処理の流れが2つになる | |
class Car extends Thread // スレッドクラスを拡張 | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
System.out.println(name+"の処理をしています。"); | |
} | |
} | |
} | |
class Sample1 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
car1.start(); | |
for(int i=0; i<5; i++) { | |
System.out.println("main()の処理をしています。"); | |
} | |
} | |
} |
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
// 処理の流れが3つになる | |
class Car extends Thread // スレッドクラスを拡張 | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
System.out.println(name+"の処理をしています。"); | |
} | |
} | |
} | |
class Sample2 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
car1.start(); | |
Car car2 = new Car("2号車"); | |
car2.start(); | |
for(int i=0; i<5; i++) { | |
System.out.println("main()の処理をしています。"); | |
} | |
} | |
} |
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 Car extends Thread // スレッドクラスを拡張 | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
try { | |
sleep(1000); | |
System.out.println(name+"の処理をしています。"); | |
} | |
catch(InterruptedException e) {} | |
} | |
} | |
} | |
class Sample3 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
car1.start(); | |
for(int i=0; i<5; i++) { | |
System.out.println("main()の処理をしています。"); | |
} | |
} | |
} |
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 Car extends Thread // スレッドクラスを拡張 | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
System.out.println(name+"の処理をしています。"); | |
} | |
} | |
} | |
class Sample4 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
car1.start(); | |
for(int i=0; i<5; i++) { | |
try { | |
Thread.sleep(1000); | |
System.out.println("main()の処理をしています。"); | |
} | |
catch(InterruptedException e) {} | |
} | |
} | |
} |
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 Car extends Thread // スレッドクラスを拡張 | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
System.out.println(name+"の処理をしています。"); | |
} | |
} | |
} | |
class Sample5 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
car1.start(); | |
try { | |
car1.join(); | |
} | |
catch(InterruptedException e) {} | |
System.out.println("main()の処理を終わります。"); | |
} | |
} |
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 Car implements Runnable // Runnableインターフェイスを拡張 | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
System.out.println(name+"の処理をしています。"); | |
} | |
} | |
} | |
class Sample6 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
// Threadクラスのオブジェクトを作成する | |
Thread th1 = new Thread(car1); | |
th1.start(); | |
for(int i=0; i<5; i++) { | |
System.out.println("main()の処理をしています。"); | |
} | |
} | |
} |
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 Company | |
{ | |
private int sum = 0; | |
public synchronized void add(int a) | |
{ | |
int tmp = sum; | |
System.out.println("現在、合計額は"+tmp+"円です"); | |
System.out.println(a+"稼ぎました"); | |
tmp = tmp + a; | |
System.out.println("合計額は"+tmp+"円にします"); | |
sum = tmp; | |
} | |
} | |
// 運転手クラス | |
class Driver extends Thread | |
{ | |
private Company comp; | |
public Driver(Company c) | |
{ | |
comp = c; | |
} | |
public void run() | |
{ | |
for (int i=0; i<3; i++) { | |
comp.add(50); | |
} | |
} | |
} | |
class Sample7 | |
{ | |
public static void main(String[] args) | |
{ | |
Company cmp = new Company(); | |
Driver drv1 = new Driver(cmp); | |
drv1.start(); | |
Driver drv2 = new Driver(cmp); | |
drv2.start(); | |
} | |
} |
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 Car implements Runnable | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
System.out.println(name+"の処理をしています。"); | |
} | |
} | |
} | |
class SampleP2 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
Thread th1 = new Thread(car1); | |
th1.start(); | |
for(int i=0; i<5; i++) { | |
System.out.println("main()の処理をしています。"); | |
} | |
} | |
} |
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 Car implements Runnable | |
{ | |
private String name; | |
public Car(String nm) | |
{ | |
name = nm; | |
} | |
public void run() | |
{ | |
for(int i=0; i<5; i++) { | |
try { | |
Thread.sleep(1000); | |
System.out.println(name+"の処理をしています。"); | |
} | |
catch(InterruptedException e) {} | |
} | |
} | |
} | |
class SampleP3 | |
{ | |
public static void main(String[] args) | |
{ | |
Car car1 = new Car("1号車"); | |
Thread th1 = new Thread(car1); | |
th1.start(); | |
for(int i=0; i<5; i++) { | |
System.out.println("main()の処理をしています。"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment