写2个线程,其中一个线程打印1-52,另一个线程打印A-Z,打印顺序应该是12A34B56C...5152Z.

本文介绍了如何创建两个线程,一个打印数字1到52,另一个打印字母A到Z,实现交替打印的效果,如12A34B...5152Z,展示了线程间的协调与同步。
摘要由CSDN通过智能技术生成

打印数字:

package com.sa.thread.练习.练习1;

import javax.swing.plaf.synth.SynthViewportUI;

/**
 * Created by 123 on 2018-07-26
 */
public class PrintNum extends Thread{
    public static boolean flag=true;
    private int start;
    private int end;
    private String threaName;

    public PrintNum() {
    }

    public PrintNum(int start, int end,String threaName) {
        this.start = start;
        this.end = end;
        this.threaName=threaName;
    }

    @Override
    public  void run() {

        for(int i=0;start<=end;i++){
            m1(i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public  synchronized void m1(int i){
        System.out.println("m1--NO------------------"+i+"次");
        if(flag==true) {
            for (int j = 0; j < 2; j++) {
                System.out.println("开始打印数字:"+start);
                start++;
            }
            flag = false;
        }
    }
}

打印字符:

package com.sa.thread.练习.练习1;

import static com.sa.thread.练习.练习1.PrintNum.flag;

/**
 * Created by 123 on 2018-07-26
 */
public class PrintChar extends Thread{
    private int start;
    private int end;
    private String threaName;

    public PrintChar(char start,char end) {

    }

    public PrintChar(int start, int end,String threaName) {
        this.start = s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值