Java Swing-如何同步ArrayList
我正在尝试跨两个不同的线程获取一个列表。最初,我使用的是for循环,如下所示:Java Swing-如何同步ArrayList,java,swing,arraylist,synchronized,Java,Swing,Arraylist,Synchronized,我正在尝试跨两个不同的线程获取一个列表。最初,我使用的是for循环,如下所示: for (int i = 0; i<fighterList.size(); i++) { if (fighterList.get(i).isDestroyed() == true) fighterList.remove(i); } 或 Iterator Iterator=fighter.Iterator(); while(iterator.hasNext()) if(iterator.next().r
for (int i = 0; i<fighterList.size(); i++) {
if (fighterList.get(i).isDestroyed() == true) fighterList.remove(i);
}
或
Iterator Iterator=fighter.Iterator();
while(iterator.hasNext())
if(iterator.next().returnSomething()==false)iterator.next().dosomethod();
}
然而,这两种方法似乎都不起作用。做这件事的“正确”方法是什么?是否有更好的文档可用?嗯,通常在需要线程安全代码时进行同步, 这意味着当这个线程进入cpu时,没有其他线程 也将尝试到达那里,这也称为“获取对象上的锁”, “获取锁”、“锁定对象”或“同步对象” 当然,您应该始终记住,作为java设计人员,没有任何东西是可以保证的 经常说。。 无论如何,为了运行线程并将其与包含该线程的类同步 应执行以下两个操作之一: 1.扩展线程类 或 2.实现可运行接口。 此类还应重写在Thread类中定义的抽象方法 在名为“public void run(){}”的可运行接口中, 它实际上是线程的“主”类 让我们算一算。这比听起来容易得多:
class ThreadPractice implements Runnable{
@override //This annotation denotes that it overrides an abstract method from
// the Runnable interface.
public void run(){ // We've promised
coutingStuff() ; // Calling the synchronized method from the run() method.
}
public void synchronized coutingStuff(){// now this is a synchronized method...
for(int i=0; i<100; i++){// Creating a for loop that counts from 0 to 99...
System.out.println("this is the "+i+" time that this loop runs");
// Now, putting the thread to sleep for 1 second:
try{
Thread.sleep(1000);
}catch(InterruptedException iex){
System.out.println(iex.getMessage());
}
}// End for loop
}// End public void synchronized coutingStuff(){....
public static void main(String[] args){
//Now creating a thread object:
Runnable rnb = new ThreadPractice();
Thread count = new Thread(rnb);
count.start(); // starts the thread that contains
// the synchronized method.
}// End main
}// End of class ThreadPractice
class ThreadPractice实现可运行{
@override//此注释表示它重写了
//可运行接口。
public void run(){//我们已经答应了
coutingStuff();//从run()方法调用同步方法。
}
public void synchronized coutingStuff(){//现在这是一个同步方法。。。
对于(int i=0;i“这两个似乎都不起作用”不是问题描述。您需要描述您想要实现的行为以及获得的结果。还包括。即使没有多个线程访问fighterList
,第一个示例也会失败,因为它在迭代列表时修改了列表,这是禁止的!您可以使用同步的
包装器包装,请参阅@MadProgrammer如果我没记错的话,synchronizedList
使单个操作原子化(添加、包含、删除等),因此OP还需要为这些操作集提供单独的同步。根据OP真正想要实现的CopyOnWriteArrayList也可以是一个选项。无论如何,为了回答这个问题,我们(或者至少我)需要更多的信息。@Pshemo Yea因为缺少上下文;)-另外,根据具体情况,同步操作(调用了方法)可能会更好……但是上下文是王者;)但是synchronized
方法不是基于类的实例锁定的吗?
Iterator<Fighter> iterator = fighter.iterator();
while (iterator.hasNext())
if (iterator.next().returnSomething() == false) iterator.next().doSomeMethod();
}
class ThreadPractice implements Runnable{
@override //This annotation denotes that it overrides an abstract method from
// the Runnable interface.
public void run(){ // We've promised
coutingStuff() ; // Calling the synchronized method from the run() method.
}
public void synchronized coutingStuff(){// now this is a synchronized method...
for(int i=0; i<100; i++){// Creating a for loop that counts from 0 to 99...
System.out.println("this is the "+i+" time that this loop runs");
// Now, putting the thread to sleep for 1 second:
try{
Thread.sleep(1000);
}catch(InterruptedException iex){
System.out.println(iex.getMessage());
}
}// End for loop
}// End public void synchronized coutingStuff(){....
public static void main(String[] args){
//Now creating a thread object:
Runnable rnb = new ThreadPractice();
Thread count = new Thread(rnb);
count.start(); // starts the thread that contains
// the synchronized method.
}// End main
}// End of class ThreadPractice