多线程,实现两个线程加1,两个线程减1

    技术2022-05-11  0

    package com.thread;

    public class ThreadTest1 { private int j = 0;

     public static void main(String arg[]) {  ThreadTest1 tt = new ThreadTest1();  Run1 r1 = tt.new Run1();  Run2 r2 = tt.new Run2();  Run1 r3 = tt.new Run1();  Run2 r4 = tt.new Run2();

      r1.start();  r2.start();  r3.start();  r4.start();

     }

     class Run1 extends Thread {  public synchronized void run() {   for (int i = 0; i < 10; i++) {    System.out      .println(Thread.currentThread().getName() + ":" + j++);   }  } }

     class Run2 extends Thread {  public synchronized void run() {   for (int i = 0; i < 10; i++) {    System.out      .println(Thread.currentThread().getName() + ":" + j--);   }  } }}

     


    最新回复(0)