一、问题描述:
用Java实现多线程并发执行。三个线程,编号分别为1, 2, 3,要求各线程依次输出thread1, thread2, thread3, thread1, thread2, thread3, thread1, thread2, thread3, ...。
二、问题分析:
通过类的对象成员标志线程,通过类的静态对象成员控制线程输出操作执行顺序。
三、代码实现:
1. 代码实现1:
public class ThreadTest_001 {
public static void main(String[] args) {
int n = 3;
for (int i = 1; i <= n; ++i) {
Thread t = new Test1(i);
t.start();
}
}
}
class MyTest {
static MyTest single_instanceMyTest;
int a;
MyTest(int aa) {
a = aa;
}
static MyTest getInstance() {
if (single_instanceMyTest == null) {
return new MyTest(1);
} else {
return single_instanceMyTest;
}
}
}
class Test1 extends Thread {
static MyTest sTest = MyTest.getInstance();
static int n = 0;
int a;
Test1(int aa) {
a = aa;
++n;
}
@Override
public void run() {
super.run();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (sTest) {
if (a == sTest.a) {
System.out.println("thread" + a);
sTest.a = sTest.a % n + 1;
}
}
}
}
}
2. 代码实现2:
public class ThreadTest_001 {
public static void main(String[] args) {
Thread t1 = new Test1(1);
Thread t2 = new Test1(2);
Thread t3 = new Test1(3);
t1.start();
t2.start();
t3.start();
}
}
class MyTest {
static MyTest single_instanceMyTest;
int a;
MyTest(int aa) {
a = aa;
}
static MyTest getInstance() {
if (single_instanceMyTest == null) {
return new MyTest(1);
} else {
return single_instanceMyTest;
}
}
}
class Test1 extends Thread {
static MyTest sTest = MyTest.getInstance();
int a;
Test1(int aa) {
a = aa;
}
@Override
public void run() {
super.run();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (sTest) {
if (a == 1 && sTest.a == 1) {
System.out.println("thread1");
sTest.a = 2;
}
if (a == 2 && sTest.a == 2) {
System.out.println("thread2");
sTest.a = 3;
}
if (a == 3 && sTest.a == 3) {
System.out.println("thread3");
sTest.a = 1;
}
}
}
}
}
3. 代码实现3:
public class ThreadTest_001 {
public static void main(String[] args) {
Thread t1 = new Test1(1);
Thread t2 = new Test1(2);
Thread t3 = new Test1(3);
t1.start();
t2.start();
t3.start();
}
}
class MyTest {
static MyTest single_instanceMyTest;
int a;
MyTest(int aa) {
a = aa;
}
static MyTest getInstance() {
if (single_instanceMyTest == null) {
return new MyTest(1);
} else {
return single_instanceMyTest;
}
}
}
class Test1 extends Thread {
static MyTest sTest = MyTest.getInstance();
int a;
Test1(int aa) {
a = aa;
}
@Override
public void run() {
super.run();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (sTest) {
if (a == sTest.a) {
System.out.println("thread" + a);
sTest.a = sTest.a % 3 + 1;
}
}
}
}
}
4. 代码实现4:
public class ThreadTest_001 {
public static void main(String[] args) {
int n = 3;
for (int i = 1; i <= n; ++i) {
Thread t = new Test1(i);
t.start();
}
}
}
class MyTest {
static MyTest single_instanceMyTest;
int a;
MyTest(int aa) {
a = aa;
}
static MyTest getInstance() {
if (single_instanceMyTest == null) {
return new MyTest(1);
} else {
return single_instanceMyTest;
}
}
}
class Test1 extends Thread {
static MyTest sTest = MyTest.getInstance();
static int n = 0;
int a;
Test1(int aa) {
a = aa;
++n;
}
@Override
public void run() {
super.run();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (sTest) {
if (a == sTest.a) {
System.out.println("thread" + a);
sTest.a = sTest.a % n + 1;
}
}
}
}
}
改变n的初值,可以控制不同的进程个数,并保证各进程按顺序执行输出动作。
四、测试结果:
thread1
thread2
thread3
thread1
thread2
thread3
thread1
thread2
thread3
thread1
thread2
thread3
thread1
thread2
thread3
thread1
thread2
thread3
thread1
thread2
thread3
五、总结:
初次写Java代码,初次写Java多线程,不知正确与否。还请各位多多指正。