Tag: multithreading

Component.setBounds调用Component.repaint?

所以我正在制作游戏,我有EnemyAI和player ,他们都扩展了JPanel 。 世界有一个null布局,所以我使用setBounds(); “移动”(我实际上只是移动世界图像) entities ( player和AI )并正确定位它们。 但是当我添加(看起来像是我测试的最小数量)5时,它会完全调用repaint() 。 这使得玩家在视觉上行走到位。 我添加的实体越多,间隔越快(即5个实体调用repaint()比500慢很多)。 注意:下面的类window只是一个JFrame 。 主类: public class Game(){ public static boolean fighting = false; public static void startGame(){ WorldPanel game = new WorldPanel(); game.setPreferredSize(new Dimension(window.getWidth(), window.getHeight())); PlayerPane player = new PlayerPane(32,32, “Player 1”); game.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent arg0) { if(fighting == false){ move(player, […]

使用multithreading读取单个文件:应该加快速度吗?

我正在读一个包含500000行的文件。 我正在测试看多个线程如何加快进程…. private void multiThreadRead(int num){ for(int i=1; i”+e.getMessage()); e.printStackTrace(); } } }; } private void sequentialRead(int num){ try{ long startTime = System.currentTimeMillis(); System.out.println(“Start time:”+startTime); for(int i =0; i < num; i++){ RandomAccessFile raf = new RandomAccessFile("./src/test/test1.csv","r"); String line = ""; while((line = raf.readLine()) != null){ //System.out.println(line); } } long elapsedTime = System.currentTimeMillis() – startTime; […]

通过不同的线程访问变量和摆动组件

这个问题与我在这里问的问题有些关系。 现在,我有一个类“Controller”,它由main方法和所有swing组件组成。 有一个名为“VTOL”的类,它由一个名为“altitude”的变量组成(我现在已声明此变量为volatile)。 这是一个由在后台运行的线程组成的类: import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Vineet */ public class Gravity extends Thread { String altStr; double alt; Controller ctrl = new Controller(); @Override public void run() { while (true) { alt=VTOL.altitude; System.out.println(alt); alt = alt-0.01; VTOL.altitude= (int) alt; altStr=new Integer(VTOL.altitude).toString(); ctrl.lblAltitude.setText(altStr); try { Thread.sleep(10); } catch (InterruptedException e) […]

使用线程捕获进程输出

我正在使用一个线程来捕获进程的流输出,然后将该流输出到eclipse控制台。 我的问题是何时终止正在进行流输出的线程。 Thread t = new Thread(new Runnable(){ private boolean isProcessDone(Process p) { //not sure what to do here } public void run() { Process p = Runtime.getRuntime().exec(“executable with output”); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader error = new BufferedReader (new InputStreamReader(p.getErrorStream())); while ( !isProcessDone(p) ) { String line; if( (line = input.readLine()) != […]

如何等待一个产生它自己的线程的线程?

我正在尝试测试一个在单独的线程中工作的方法,简化它是这样的: public void methodToTest() { Thread thread = new Thread() { @Override public void run() { Clazz.i = 2; } }; thread.start(); } 在我的unit testing中,我想测试Clazz.i == 2,但我不能这样做,因为我认为断言是在线程更改值之前运行的。 我想使用另一个线程来测试它,然后使用join等待,但它仍然无法正常工作。 SSCCE: @Test public void sscce() throws InterruptedException { Thread thread = new Thread() { @Override public void run() { methodToTest() } }; thread.start(); thread.join(); AssertEquals(2, Clazz.i); } […]

Executor和PriorityBlockingQueue上的ASyncTask

我正在尝试使一些ASyncTask同时以优先级运行。 我正在创建一个带有PriorityBlockingQueue的ThreadPoolExecutor,并且propper比较器适用于标准的Runnables。 但是在打电话时 new Task().executeOnExecutor(threadPool, (Void[]) null); PriorityBlockingQueue的比较器接收ASyncTask的Runnable(私有)内部(在源代码中称为mFuture),因此在比较器中我无法识别runnables或读取“priority”值。 我怎么解决这个问题? 谢谢

是什么让线程的执行顺序变得不可预测?

是什么让线程的执行顺序变得不可预测? 调度程序在某些时候是使用随机数还是检查系统资源,或者查看哪个线程等待了足够长的时间或者……?

为什么Thread.stop()如此危险

为什么Thread.stop()如此危险? 为什么建议使用Thread.interrupted()代替? 我知道stop被弃用了。 还有什么让它不安全? 有什么地方可以使用stop方法吗? 如果是这样给我一个例子。

如何等待所有线程完成执行?

假设我正在使用HTTP请求库来下载文件。 这个库使用里面的线程。 现在,我想等待主线程,直到其他线程完成执行。 通过google搜索找到的所有其他解决方案只有在我有权访问库中使用的Thread变量时才有效。 但这些都不适合我。 这是我目前使用的: package smartzero.eightnoteight.testfirebase; import com.firebase.client.AuthData; import com.firebase.client.Firebase; import com.firebase.client.FirebaseError; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print(“email: “); String email = in.nextLine(); System.out.print(“password: “); String password = in.nextLine(); Firebase fb = new Firebase(“https://nullform.firebaseio.com”); fb.authWithPassword(email, password, new AuthResultHandler()); try { Thread.sleep(1000000); […]

Java wait()不会被notify()唤醒

你好我已经调试了一整天的代码了,但我看不出哪里可能出错了。 我在主线程上使用SerialPortEventListener,在工作线程中我有一个与服务器通信的客户端套接字。 由于在这个工作线程return ,我仍然需要在主线程中完成一些总结工作,我想创建一个在主线程中等待的“伪线程”,直到从侦听器onEvent方法通知它。 但是这种伪线似乎永远等待着。 我检查了锁定的线程pseudoThread ,它们应该在Runnable和Listener类中具有相同的对象id。 显示“PseudoThread waiting”,但是从未显示PseudoThread清醒。 控制台输出显示:PseudoThread等待.. .. false通知伪线程。 PS如果我在Main类中使用public final Object lock = new Object();创建一个锁public final Object lock = new Object(); 并用main.pseudoThread替换所有main.pseudoThread ,我得到java.lang.IllegalMonitorStateException。 private class Pseudo implements Runnable{ Main main; public Pseudo(Main main) { this.main = main; } @Override public void run() { synchronized(main.pseudoThread){ try { System.out.println(“PseudoThread waiting”); main.pseudoThread.wait(); System.out.println(“PseudoThread awake”); […]