标签:线程安全 span code gen get ++ equal art ref
public class Test { public static Vector<String> data = new Vector<String>(); public static void main(String[] args) { for (int i = 0; i < 100; i++) { data.add("data" + i); } for (int i = 0; i < 3; i++) { Thread t = new Thread() { public void run() { String str = ""; while (data.size() > 0 && !(str = data.remove(0)).equals("")) { System.out.println(this.getId() + "||" + str); //暂停1秒 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; t.start(); } } }
关于集合安全:
关于创建线程:
方法一:如上,匿名的线程
方法二:创建一个类MyThread继承Thread。使用如下:
MyThread t=new MyThread();
t.start();
方法三:创建类MyThread实现接口Runnable。使用如下:
Thread t=new Thread(new MyThread);
t.start();
创建线程可参考:http://www.importnew.com/20672.html
标签:线程安全 span code gen get ++ equal art ref
原文地址:https://www.cnblogs.com/duanjt/p/9257835.html