java题目 编程题目 多线程

java题目 编程题目 多线程,第1张

public class DoubleThread {

public static void main(String[] args) {

Thread t1 = new Thread() {

@Override

public void run() {

for (char i = 'a'i <= 'z'i++) {

System.out.println(i)

}

}

}

Thread t2 = new Thread() {

@Override

public void run() {

for (char i = 'A'i <= 'Z'i++) {

System.out.println(i)

}

}

}

t1.start()

t2.start()

}

}

package com.zhidao20161220

public class Main {

    private static int count=0

    public static void main(String[] args) {

        // TODO Auto-generated method stub

      for(int i=0i<10i++)

      {

          String name = "人员"+(i+1)

          ManThread manThread = new ManThread(name)

          manThread.start()

      }

    }

    

    public static synchronized void go(String name)

    {

        count++

        System.out.println("线程"+Thread.currentThread().getName()+"执行,"+name+"正在通过山洞")

        try {

        if(count>=10)

        {

            System.out.println("全员通过程序结束")

            

        }

            Thread.sleep(5000)

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace()

        }

    }

} package com.zhidao20161220

public class ManThread extends Thread {

    private String name

    public ManThread(String name)

    {

        this.name = name

    }

    public void run() {

        Main.go(name)

    }

}

package com.demo.test/**

* 基于两个lock实现连续打印abcabc....

* @author lixiaoxi

* */public class TwoLockPrinter implements Runnable {// 打印次数

private static final int PRINT_COUNT = 10 // 前一个线程的打印锁

private final Object fontLock // 本线程的打印锁

private final Object thisLock // 打印字符

private final char printChar public TwoLockPrinter(Object fontLock, Object thisLock, char printChar) {this.fontLock = fontLock this.thisLock = thisLock this.printChar = printChar

}

@Overridepublic void run() {// 连续打印PRINT_COUNT次

for (int i = 0i <PRINT_COUNTi++) {// 获取前一个线程的打印锁

synchronized (fontLock) {// 获取本线程的打印锁

synchronized (thisLock) {//打印字符System.out.print(printChar) // 通过本线程的打印锁唤醒后面的线程

// notify和notifyall均可,因为同一时刻只有一个线程在等待thisLock.notify()

}// 不是最后一次则通过fontLock等待被唤醒// 必须要加判断,不然虽然能够打印10次,但10次后就会直接死锁

if(i <PRINT_COUNT - 1){try {// 通过fontLock等待被唤醒fontLock.wait()

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

}

}public static void main(String[] args) throws InterruptedException {// 打印A线程的锁

Object lockA = new Object() // 打印B线程的锁

Object lockB = new Object() // 打印C线程的锁

Object lockC = new Object()

// 打印a的线程

Thread threadA = new Thread(new TwoLockPrinter(lockC, lockA, 'A')) // 打印b的线程

Thread threadB = new Thread(new TwoLockPrinter(lockA, lockB, 'B')) // 打印c的线程

Thread threadC = new Thread(new TwoLockPrinter(lockB, lockC, 'C')) // 依次开启a b c线程threadA.start()

Thread.sleep(100)// 确保按顺序A、B、C执行threadB.start()

Thread.sleep(100)

threadC.start()

Thread.sleep(100)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/10959611.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存