代码如下: package com.thread.test; public class Testabc implements Runnable { //确定哪个线程在工作 private static int count = 0; //线程标志 private int id; //线程工作次数 private int pCount; //当前工作线程标志 int current; //要输出的i static int i = 0; public Testabc(int id) { this.id = id; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Thread a = new Thread(new Testabc(0),"a"); Thread b = new Thread(new Testabc(1),"b"); Thread c = new Thread(new Testabc(2),"c"); Thread d = new Thread(new Testabc(3),"d"); a.start(); b.start(); c.start(); d.start(); } public void run() { // TODO Auto-generated method stub synchronized("lock"){ for(;;){ current = count%4; if(current == id){ if(current == 0 || current == 1){ //给i自增 System.out.print(i+" "); i++; }else{ System.out.print(i+" "); i--; //i自减 } count++; pCount++; "lock".notifyAll(); if(pCount == 10)break; }else{ try{ "lock".wait(); }catch(Exception ex){ ex.printStackTrace(); } } } } } }