如何将变量传递给内部线程类?

Vim*_*deo 3 java image

我想定期将图像加载到图像项目.我的外部类正在生成URL,我需要将它传递给内部类.我该如何实现这一目标?

public class MapTimer extends TimerTask{
    public void run() {
        System.out.println("Map starting...");

        String URL=null,serverquery=null;
        try {
            sendMessage(this.message);
            item.setLabel(item.getLabel()+"start");
            serverquery=receiveMessage();
            item.setLabel(item.getLabel()+"stop");
            URL = getURL(serverquery);               // my url to be passed to innerclass
            System.out.println("URl is "+serverquery);
            item.setLabel(URL+item.getLabel());
            Thread t = new Thread() {
            public void run() {
                    item.setLabel(item.getLabel()+"6");
                    try {
                          Image image = loadImage(URL);            // using url
                          System.out.println("GEtting image....");
                          item = new ImageItem(null, image, 0, null);
                          form.append(item);
                          display.setCurrent(form);

                    } catch (IOException ioe) {
                          item.setLabel("Error1");
                    }
                    catch (Exception ioe) {
                          item.setLabel("Error1");
                    }
              }
         };
         t.start(); // write post-action user code here
     }
     catch(Exception e){
         System.out.println("Error3"+e);
     }
  }    
}
Run Code Online (Sandbox Code Playgroud)

如何将URL传递给我的innerthread类?

Rof*_*ion 5

您必须声明变量final或不使用变量,而是使用类中的字段.

public class YourClass {

private String url;

public void yourMethod {
   url = getURL(serverquery);
   System.out.println("URl is "+serverquery);
   item.setLabel(URL+item.getLabel());
   Thread t = new Thread() {
      public void run() {
         item.setLabel(item.getLabel()+"6");
         try {
           Image image = loadImage(url);            // using url
           System.out.println("GEtting image....");
           item = new ImageItem(null, image, 0, null);
           form.append(item);
           display.setCurrent(form);

    } catch (IOException ioe) {
      item.setLabel("Error1");
    }
                catch (Exception ioe) {
      item.setLabel("Error1");
    }

  }
};
t.start(); // write post-action user code here
}
Run Code Online (Sandbox Code Playgroud)

}