|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
需求如图,以下是我写的代码,实际运行只出现两个就没了,找了好久没发现问题,求助
- public class ThreadTest8 {
- public static void main(String[] args) {
- new PrintLetters().start();
- new PrintLetters().start();
- }
- }
- class PrintLetters extends Thread{
- private static boolean flag = true;
- private static char c = 'a';
- @Override
- public void run() {
- while (true){
- synchronized (this){
- for (int i=0;i<3;i++){
- if (flag) {
- System.out.println(Thread.currentThread().getName() + "-->" + c);
- }else{
- System.out.println(Thread.currentThread().getName() + "-->" + (char)(c-32));
- }
- c++;
- if (c>'z'){
- c='a';
- }
- }
- flag = !flag;
- this.notify();
- try {
- this.wait();
- this.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
复制代码 |
-
需求
-
运行情况
|