写两个线程,一个线程打印1-52,了,另一个线程打印A-Z,要求打印顺序为12A34B56C…5152Z。(线程同步加等待唤醒的协作机制)

练习多线程时发现几种方式,记录一下:

第一种:加判断条件然后用++i

public class Test {
    public static void main(String[] args) {
        Count obj = new Count();
        new Thread(() -> {
            synchronized (obj) {
                for (int i = 1; i <= 52; i++) {
                    System.out.println(i);
                    if (obj.getCount() == 0) {
                        System.out.println(++i);
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    obj.setCount(0);
                    obj.notify();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (obj) {
                for (int i = 'A'; i <= 'Z'; i++) {
                    if (obj.getCount() == 2) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println((char) i);
                    obj.setCount(2);
                    obj.notify();
                }
            }
        }).start();
    }
}

class Count {
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

第二种:和第一种大同小异,加一个i%2判断是否为零

public class PrintNum {
    public static void main(String[] args) {
        Count obj = new Count();
        new Thread(() -> {
            synchronized (obj) {
                for (int i = 1; i < 53; i++) {
                    System.out.println(i);
                    if (i % 2 == 0) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    obj.notify();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (obj) {
                for (int i = 'A'; i <= 'Z'; i++) {
                    if (obj.getCount() != 0) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println((char) i);
                    obj.setCount(1);
                    obj.notify();
                }
            }
        }).start();
    }
}
class Count{
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

第三种:则是调用方法

class Print{
    private int flag = 1;
    private int count = 1;

    public synchronized void printNum(){
        while(flag != 1) {
            //此时应该打印字母
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
            System.out.print(2*count-1);
            System.out.print(2*count);
            flag = 2;
            notify();

    }
    public synchronized void printChar(){
        while(flag != 2){
            //此时应该打印数字
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print((char)(count-1+'A'));
        //继续前行
        count++;
        flag = 1;
        notify();
    }
}
public class Test{
    public static void main(String[] args) {
        Print print = new Print();
        new Thread(()->{
            for(int i = 0;i<26;i++){
                print.printNum();
            }
        }).start();
        new Thread(()->{
            for(int i = 0;i<26;i++){
                print.printChar();
            }
        }).start();
    }
}

第三种方法来自main--的博客

写两个线程,一个线程打印 1~ 52,另一个线程打印A~Z, 打印顺序是12A34B...5152Z;main--的博客icon-default.png?t=M666https://blog.csdn.net/qq_41420688/article/details/84334894

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用信号量来实现线程的同步顺序控制。 首先定义两个信号量,一个用于控制数字线程的输出,一个用于控制字母线程的输出。初始时,数字线程的信号量为1,字母线程的信号量为0。 然后在数字线程中循环输出1~52,每输出一个数字后,将字母线程的信号量1,表示可以输出字母了。然后等待字母线程的信号量减1,表示字母线程已经输出完毕,可以继续输出数字。 在字母线程中循环输出a~z,每输出一个字母后,将数字线程的信号量1,表示可以输出数字了。然后等待数字线程的信号量减1,表示数字线程已经输出完毕,可以继续输出字母。 这样就可以保证数字和字母的输出顺序为12a34b…5152z。 下面是示例代码: ```python import threading num_sem = threading.Semaphore(1) # 数字线程的信号量 char_sem = threading.Semaphore(0) # 字母线程的信号量 def print_num(): for i in range(1, 53): num_sem.acquire() # 等待数字线程的信号量 print(i, end='') if i % 2 == 0: char_sem.release() # 发送信号给字母线程 else: num_sem.release() # 继续等待数字线程的信号量 def print_char(): for c in 'abcdefghijklmnopqrstuvwxyz': char_sem.acquire() # 等待字母线程的信号量 print(c, end='') num_sem.release() # 发送信号给数字线程 t1 = threading.Thread(target=print_num) t2 = threading.Thread(target=print_char) t1.start() t2.start() t1.join() t2.join() ``` 输出结果为: ``` 12a34b56c78d910e1112f1314g1516h1718i1920j2122k2324l2526m2728n2930o3132p3334q3536r3738s3940t4142u4344v4546w4748x4950y5152z ``` ### 回答2: 这道题是典型的线同步问题。我们可以用多种方法来解决这个问题,比如使用信号量、互斥锁等等,这里介绍一种使用wait()和notify()方法的实现方式。 首先,我们创建两个线程类,分别用于打印数字和字母: ``` class NumberThread implements Runnable { private final Object lock; public NumberThread(Object lock) { this.lock = lock; } public void run() { synchronized (lock) { for (int i = 1; i <= 52; i += 2) { System.out.print(i + "" + (i + 1)); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } } } class LetterThread implements Runnable { private final Object lock; public LetterThread(Object lock) { this.lock = lock; } public void run() { synchronized (lock) { for (char c = 'a'; c <= 'z'; c++) { System.out.print(c); lock.notify(); try { if (c != 'z') lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } } } ``` 这里我们使用了一个共享对象lock来保证两个线程的同步。对于数字线程来说,我们先打印奇数,然后打印偶数,每打印一个数就通知字母线程,然后自己进入等待状态,等待字母线程的通知。对于字母线程来说,我们先打印一个字母,然后等待数字线程的通知,接着判断如果不是最后一个字母就进入等待状态。需要注意的是,最后两个线程都必须通知对方,否则就会出现死锁。 然后我们在主函数中创建两个线程并启动它们: ``` public static void main(String[] args) { Object lock = new Object(); NumberThread nThread = new NumberThread(lock); LetterThread lThread = new LetterThread(lock); new Thread(nThread).start(); new Thread(lThread).start(); } ``` 最后我们就可以看到程序按照题目要求打印出了1~52和a~z的序列。 这种方式虽然稍微有些繁琐,但是它比较通用,可以解决几乎所有的线同步问题。当然,对于一些简单的问题,我们可以使用更简单的方式来实现线同步,比如使用synchronized关键字。 ### 回答3: 这道题需要用到线程间的同步与互斥。可以采用信号量的方式来实现。 1.首先定义两个信号量:sem1和sem2。sem1用于控制数字线程的打印,sem2用于控制字母线程的打印。 2.在数字线程中,先打印1,然后释放sem2信号量,挂起自己等待sem1信号量。在字母线程中,先打印a,然后释放sem1信号量,挂起自己等待sem2信号量。 3.当数字线打印完52后,会释放sem2信号量,而字母线程会因为sem2已经释放而继续执行打印b。 4.这时候再让数字线程获得sem1信号量继续打印3,直到打印完52,释放sem2信号量。 5.字母线程依次打印c~z,直到全部完成。 代码如下: ``` #include <pthread.h> #include <semaphore.h> #include <stdio.h> sem_t sem1, sem2; void* print_num(void* args) { for (int i = 1; i <= 52; i += 2) { sem_wait(&sem2); printf("%d", i); printf("%d", i + 1); sem_post(&sem1); } return NULL; } void* print_char(void* args) { for (char c = 'a'; c <= 'z'; c++) { sem_wait(&sem1); printf("%c", c); sem_post(&sem2); } return NULL; } int main() { pthread_t t1, t2; sem_init(&sem1, 0, 0); sem_init(&sem2, 0, 1); pthread_create(&t1, NULL, print_num, NULL); pthread_create(&t2, NULL, print_char, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); sem_destroy(&sem1); sem_destroy(&sem2); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值