图片的循环显示(图像的使用,线程)

其实,这个程序中有点不明白的地方,就是其中Thread runner;为什么我用Thread runner=new Thread ();定义的时候就不对呢?这个时候,图片就不会循环显示,只是在一张上不动。希望帮我解决一下。

这其中还遇到了一个问题,那就是在判断runner是否为空的时候,犯了一个小错误,导致找了好长时间,

不是:

  if(runner==null){
   Thread runner=new Thread (this);  }

而是:

  if(runner==null){
   runner=new Thread (this);  }
解释就不需要了吧,runner必须是同一个变量。

import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.net.*;
public class DongHua extends JApplet implements Runnable {
 
 Image[] labels=new Image[3];
  int current=0;
  Thread runner;//这个地方就是这样写,不知道为什么
 public void init(){
  

//这是今天的新知识:图像的建立和显示。
  
   
labels[0]=getImage(getCodeBase(),"right1.jpg");

   labels[1]=getImage(getCodeBase(),"right.jpg");
                         labels[2]=getImage(getCodeBase(),"right2.jpg");  
 }
 
 public void start(){
  if(runner==null){
   runner=new Thread (this);//这个地方就要这样写,不是新建runner,而是以前的runner=!
  }
  runner.start();
 }
 public void run(){
  Thread thisthread=Thread.currentThread();
  while (runner==thisthread){
   repaint();
   current++;
   if(current>2){
    current=0;
   }
   
   try{
    Thread.sleep(1000);
   }
   catch (InterruptedException e){}
    
  }
  
 }
 public void stop(){
  if(runner!=null){runner=null;
 }
 }
 public void paint(Graphics screen){
  super.paint(screen);
  Graphics2D screen2D=(Graphics2D)screen;
 
  screen2D.drawImage(labels[current],0,0,this);
  
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在Python中循环开启线程绑定GPU调用图像识别程序,可以使用`threading`库来创建和管理线程。以下是一个简单的示例代码,展示了如何在循环中创建线程并绑定GPU调用图像识别程序: ```python import threading # 定义一个图像识别函数 def recognize_image(image): # 在这里编写你的图像识别代码 # ... pass # 定义一个线程类 class ImageRecognitionThread(threading.Thread): def __init__(self, image): threading.Thread.__init__(self) self.image = image def run(self): # 在线程中绑定GPU并调用图像识别程序 # ... recognize_image(self.image) # 定义要处理的图像列表 images = [...] # 循环创建并启动线程 for image in images: thread = ImageRecognitionThread(image) thread.start() # 等待所有线程执行完毕 for thread in threading.enumerate(): if thread is not threading.currentThread(): thread.join() ``` 在上述示例代码中,我们定义了一个`ImageRecognitionThread`类,继承自`threading.Thread`类。每个线程都会绑定一个图像,并在`run`方法中调用`recognize_image`函数进行图像识别操作。 然后,我们通过循环创建并启动线程,每个线程都会处理不同的图像。最后,使用`threading.enumerate`方法来获取当前所有的线程,并调用`join`方法等待所有线程执行完毕。 需要注意的是,由于Python的全局解释器锁(GIL)的限制,多线程并不能真正实现并行计算。因此,如果你希望充分利用GPU资源并实现并行计算,可以考虑使用`multiprocessing`库创建子进程来代替多线程。每个子进程都有自己独立的解释器和内存空间,可以充分利用多核CPU和GPU资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值