实现Java用两个线程交替打印数字和字母的方法,有很多种。下面给出两种简单明了的方法。
方式一: 使用synchronized关键字
首先,我们定义一个共享的线程类,需要一个计数用的整数类型变量、一个布尔类型的打印数字的标记、线程的名称及一个打印方法。
public class ShareThread {
private int count = 1;
private boolean flag = false;
private String name;
public ShareThread(String name) {
this.name = name;
}
public synchronized void printNum() {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + ": " + count);
count++;
flag = true;
notify();
}
public synchronized void printChar() {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + ": " + (char) (count + 64));
count++;
flag = false;
notify();
}
}
在这个类中,我们通过synchronized关键字来保证线程安全,并使用wait()和notify()方法来确保交替打印数字和字母。
接下来,我们定义两个线程,一个线程用来打印数字,另一个用来打印字母,通过实例化ShareThread类来获取线程。
public class PrintDemo {
public static void main(String[] args) {
ShareThread shareThread = new ShareThread("Thread-");
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 26; i++) {
shareThread.printNum();
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 26; i++) {
shareThread.printChar();
}
}
});
thread1.start();
thread2.start();
}
}
这个程序的输出如下:
Thread-1: 1
Thread-2: A
Thread-1: 2
Thread-2: B
Thread-1: 3
Thread-2: C
Thread-1: 4
Thread-2: D
Thread-1: 5
Thread-2: E
Thread-1: 6
Thread-2: F
Thread-1: 7
Thread-2: G
Thread-1: 8
Thread-2: H
Thread-1: 9
Thread-2: I
Thread-1: 10
Thread-2: J
Thread-1: 11
Thread-2: K
Thread-1: 12
Thread-2: L
Thread-1: 13
Thread-2: M
Thread-1: 14
Thread-2: N
Thread-1: 15
Thread-2: O
Thread-1: 16
Thread-2: P
Thread-1: 17
Thread-2: Q
Thread-1: 18
Thread-2: R
Thread-1: 19
Thread-2: S
Thread-1: 20
Thread-2: T
Thread-1: 21
Thread-2: U
Thread-1: 22
Thread-2: V
Thread-1: 23
Thread-2: W
Thread-1: 24
Thread-2: X
Thread-1: 25
Thread-2: Y
Thread-1: 26
方式二: 使用 Lock 和 Condition 接口
这种方式和上面的方式类似,只不过使用了 Lock 和 Condition 接口。
首先,我们定义一个共享的线程类,需要一个计数用的整数类型变量、一个布尔类型的打印数字的标记、线程的名称及一个打印方法。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ShareThread {
private int count = 1;
private boolean flag = false;
private String name;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public ShareThread(String name) {
this.name = name;
}
public void printNum() {
lock.lock();
try {
if (flag) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + ": " + count);
count++;
flag = true;
condition.signalAll();
} finally {
lock.unlock();
}
}
public void printChar() {
lock.lock();
try {
if (!flag) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + ": " + (char) (count + 64));
count++;
flag = false;
condition.signalAll();
} finally {
lock.unlock();
}
}
}
这个类的实现基本和上面的方式相似,只不过使用了Lock和Condition接口来实现线程的同步。
接下来,我们定义两个线程,一个线程用来打印数字,另一个用来打印字母,通过实例化ShareThread类来获取线程。
public class PrintDemo2 {
public static void main(String[] args) {
ShareThread shareThread = new ShareThread("Thread-");
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 26; i++) {
shareThread.printNum();
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 26; i++) {
shareThread.printChar();
}
}
});
thread1.start();
thread2.start();
}
}
这个程序的输出和上面的程序相同,输出如下:
Thread-1: 1
Thread-2: A
Thread-1: 2
Thread-2: B
Thread-1: 3
Thread-2: C
Thread-1: 4
Thread-2: D
Thread-1: 5
Thread-2: E
Thread-1: 6
Thread-2: F
Thread-1: 7
Thread-2: G
Thread-1: 8
Thread-2: H
Thread-1: 9
Thread-2: I
Thread-1: 10
Thread-2: J
Thread-1: 11
Thread-2: K
Thread-1: 12
Thread-2: L
Thread-1: 13
Thread-2: M
Thread-1: 14
Thread-2: N
Thread-1: 15
Thread-2: O
Thread-1: 16
Thread-2: P
Thread-1: 17
Thread-2: Q
Thread-1: 18
Thread-2: R
Thread-1: 19
Thread-2: S
Thread-1: 20
Thread-2: T
Thread-1: 21
Thread-2: U
Thread-1: 22
Thread-2: V
Thread-1: 23
Thread-2: W
Thread-1: 24
Thread-2: X
Thread-1: 25
Thread-2: Y
Thread-1: 26
综上所述,以上是Java用两个线程交替打印数字和字母的两种实现方式及代码示例。两种方法都有自己的优劣,在实际应用中需要考虑到程序的性能和复杂度等方面来选择合适的实现方式。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java 用两个线程交替打印数字和字母 - Python技术站