简体   繁体   English

忽略Java4中的变量声明

[英]ignoring variable declaration in java4

I have an application which has to be supported in java4 and java5. 我有一个必须在java4和java5中受支持的应用程序。 If it is java 5 execution env i want to add more functionality. 如果是Java 5执行环境,我想添加更多功能。

public class Test
{
    private ScheduledExecutorService scheduler;
    private ScheduledFuture<?> scheduledFuture;

    private boolean java4ExecEnv;

    public Test()
    {
        in the manifest.mf file I have something like Bundle-RequiredExecutionEnvironment, using this i am setting java4ExecEnv
        java4ExecEnv = true/false;
    }

    method1()
    {
        if(!java4ExecEnv)
            scheduledFuture = scheduler....
    }

    method2()
    {
        if(!java4ExecEnv)
            scheduledFuture....
    }
}

In the method1 and method2 I can avoid the calls to scheduler and scheduledFuture if it is a java4 version. 在method1和method2中,如果它是java4版本,则可以避免调用Scheduler和ScheduledFuture。 But is there any way to avoid instance variable declaration(scheduler,scheduledFuture) if the execution environment is java4. 但是,如果执行环境为java4,有什么方法可以避免实例变量声明(scheduler,scheduledFuture)。

Or should I look for the alternative things which are supported in both java4 and 5. 或者我应该寻找java4和5中都支持的替代方法。

If it is java 5 execution env i want to add more functionality 如果是Java 5执行环境,我想添加更多功能

Java 5 and Java 1.4 have different APIs. Java 5和Java 1.4具有不同的API。 While there are mostly similarities, if you are planning to target a particular version, this has to be done at compile time and not in the code itself. 尽管大多数相似之处,但是如果您打算针对特定版本,则必须在编译时而不是在代码本身中完成。 You can use System.getProperty to get the version, but Java is not like C/C++ with conditional #define 's. 您可以使用System.getProperty来获取版本,但是Java与带有条件#define的C / C ++不同。 So if code is compiled on a higher JRE, it's not going to run on a lower JRE. 因此,如果代码在较高的JRE上编译,则不会在较低的JRE上运行。

Here is some demo code that will run on both Java 1.4 and Java 5: 这是一些可以在Java 1.4和Java 5上运行的演示代码:

import java.util.concurrent.*;

public class Demo implements Runnable {
  public void run() { System.out.println("Executing"); }

  public static interface Runner { void run(Runnable r); }

  private static class Runner4 implements Runner {
    public void run(Runnable r) {
      System.out.println("Java 1.4");
      new Thread(r).start();
    }
  }

  private static class Runner5 implements Runner {
    ExecutorService executor = Executors.newCachedThreadPool();

    public void run(Runnable r) {
      System.out.println("Java 5");
      executor.submit(r);
    }
  }

  public static Runner loadRunner() {
    try {
      Demo.class.forName("java.util.concurrent.ExecutorService");
      return new Runner5();
    } catch (ClassNotFoundException e) {
      return new Runner4();
    }
  }

  public static void main(String[] args) { loadRunner().run(new Demo()); }
}

This code must be compiled using Java 5 or above with a target version of 1.4. 此代码必须使用Java 5或更高版本( 目标版本为1.4)进行编译。 It checks the classpath for the ExecutorService type and falls back to an alternative if it is not present. 它检查ExecutorService类型的类路径,如果不存在,则退回到备用路径。

If this is a non-trivial project I would make the Java 5 dependent code into some sort of optional plugin and compile the bulk of the code against the Java 1.4 runtime library. 如果这不是一个无关紧要的项目,我将使Java 5依赖的代码成为某种可选的插件,并根据Java 1.4运行时库编译大部分代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM