使用2个线程,第1个线程将以正向顺序显示数组列表项目,第2个线程 线程将以相反的顺序显示列表项。
答案 0 :(得分:0)
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
public class test {
public static void main(String[] args) throws IOException, ParseException, ClassNotFoundException{
ArrayListThred Alt = new ArrayListThred();
Alt.start();
Thread th =new Thread (new Runnable(){
public void run(){
System.out.println("Starting ArrayListThred 2 " );
ArrayList<String> al=new ArrayList<String>();//creating arraylist
al.add("Ravi");//adding object in arraylist
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
for(int i=(al.size()-1); i>=0;i--)
{
System.out.println(al.get(i));
}
}
});
th.start();
}
}
class ArrayListThred extends Thread
{
Thread t;
// int number = 0;
int sum = 0;
static int total;
public ArrayListThred() {
System.out.println("Creating ArrayListThred... ");
}
public void run()
{
ArrayList<String> al=new ArrayList<String>();//creating arraylist
al.add("Ravi");//adding object in arraylist
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
public void start ()
{
System.out.println("Starting ArrayListThred " );
if (t == null)
{
// t = new Thread (this, number);
t = new Thread(this);
t.start ();
}
}
}