Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数类似于java中的fork()_Java_Android_Multithreading_Android Fragments_Fork - Fatal编程技术网

函数类似于java中的fork()

函数类似于java中的fork(),java,android,multithreading,android-fragments,fork,Java,Android,Multithreading,Android Fragments,Fork,我正在用Android编写一个应用程序,我面临以下问题 我必须重写一个内置函数Fragment-getItem(int-position),以返回图像的位图,但由于图像非常大,加载需要花费大量时间。因此,UI的响应有点慢。 要解决这个问题,我认为可以像下面的代码中那样解决。它基本上会立即返回空白图像,当加载真实图像时,它会返回正确的图像 @Override public Fragment getItem(final int position) { pid = fork()

我正在用Android编写一个应用程序,我面临以下问题

我必须重写一个内置函数
Fragment-getItem(int-position)
,以返回图像的位图,但由于图像非常大,加载需要花费大量时间。因此,UI的响应有点慢。 要解决这个问题,我认为可以像下面的代码中那样解决。它基本上会立即返回空白图像,当加载真实图像时,它会返回正确的图像

@Override
    public Fragment getItem(final int position) {
        pid = fork();  //NOT POSSIBLE / ERROR
        if (!pid)
            return  (code for empty image);
        return new ScreenSlidePageFragment(files[position].getAbsolutePath(), width, height);
    }
但是,我不知道Java中有类似于
fork()
的函数。
你能帮我解决这个问题吗,谢谢你,在Java中,你不用分叉新的进程,而是为这样的情况打开线程。有关如何执行此操作的详细信息,请参阅。

您可以定义AsynTask,并在实现其功能时执行以下操作

public class imageLoader extends AsyncTask<Void, Void, Void>{

 protected preExecute(){
  //load the blank image
  //this function is executed on the UI thread
 }

 protected doInBackground(){
  //load the real one
  //that one is not executed on the UI thread
 }

 protected postExecute(){
  //set the new image 
  //this one is executed on the UI thread
 }
}
公共类imageLoader扩展异步任务{
受保护的预执行(){
/加载空白图像
//此函数在UI线程上执行
}
受保护的doInBackground(){
//加载真实的一个
//该线程不会在UI线程上执行
}
受保护的postExecute(){
//设置新图像
//这是在UI线程上执行的
}
}
在Java中,您可以使用Thread类来实现这一点。但是,如果您正在为Android开发,您可能会考虑使用AsYcTebug,并参考官方Android开发者对

的培训。