import java.util.ArrayList;
import java.util.List;
public class Testing {
private List<String> list = new ArrayList<String>();
public class cThread extends Thread{
public void run(){
while(true){
synchronized (list){
System.out.println("thread on "+System.currentTimeMillis());
for(String str : list){
try {
sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}
}
}
}
}
public void testing(){
new cThread().start();
for(int i=0;i<10;i++){
synchronized (list){
System.out.println("add "+i+" on "+System.currentTimeMillis());
list.add(i+"");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] argv){
new Testing().testing();
}
}
import java.util.List;
public class Testing {
private List<String> list = new ArrayList<String>();
public class cThread extends Thread{
public void run(){
while(true){
synchronized (list){
System.out.println("thread on "+System.currentTimeMillis());
for(String str : list){
try {
sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str);
}
}
}
}
}
public void testing(){
new cThread().start();
for(int i=0;i<10;i++){
synchronized (list){
System.out.println("add "+i+" on "+System.currentTimeMillis());
list.add(i+"");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] argv){
new Testing().testing();
}
}