Skip to content

Instantly share code, notes, and snippets.

@wfwei
Last active December 15, 2015 09:19
Show Gist options
  • Select an option

  • Save wfwei/5237380 to your computer and use it in GitHub Desktop.

Select an option

Save wfwei/5237380 to your computer and use it in GitHub Desktop.
启动三个线程,id分别是0,1,2,输出012012012
public class ThreeThread {
public static Long count = new Long(0);
public static void main(String args[]) {
for (int i = 0; i < 3; i++) {
Thread t = new Thread() {
@Override
public void run() {
int c = 3;
while (c>0)
synchronized (count) {
long id = Long.valueOf(Thread.currentThread()
.getName());
if (count%3 == id) {
System.out.print(id);
count++;
c--;
}
}
}
};
t.setName(i + "");
t.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment