如何使线程一系列工作 [英] How to make threads work in Series

查看:182
本文介绍了如何使线程一系列工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 CameraApp 。并获得字节[] 相机$ P $在 pviewCallback上previewFrame(字节的ByteArray [],相机摄像头)

I have created a CameraApp. And getting byte[] cameraPreviewCallback in onPreviewFrame(byte byteArray[] , Camera camera)

我将这些的字节数组 RGB ,并做了很多的东西太多。所以它是很慢。所以我想我应该从的java.util.concurrent 包中受益。但没有找到一个简单的方法来处理我想要什么。

I am converting these byteArray to rgb and doing a lot of stuff too. So It is working slowly. So I think I should get benefit from java.util.concurrent package. But didn't find an easy way to handle what I want.

我要多线程连续做我的工作,但每次一可以设置位图 surfaceView 。而且还必须检查:

I want multiple threads to do my job serially but each time one can set bitmap to surfaceView. And also have to check that:

No Old byte of Array shouldn't be draw on surfaceView.

我想我必须写code。使用一个同步锁。并检查一个布尔变量 isWorkingOnFrame 真正

有人能解释我如何使用多个线程系列,但在同一时间可以做其工作。

Can someone explain me how to use multiple threads in series but at one time one can do its work.

private android.hardware.Camera.PreviewCallback previewCallback = new android.hardware.Camera.PreviewCallback()
    {
        public void onPreviewFrame(byte abyte0[] , Camera camera)
        {   
            try
            {
               Thread.sleep(1L);
            }
            catch (InterruptedException interruptedexception)
            {
                return;
            }

            int[] rgbData =  YuvUtils.myDecodeGreyscale(abyte0, widthPreview, heightPreview, imageBrightness, nonGreencolorOffset, greenColorOffset);  ///it is working...

            editedBitmap.setPixels(rgbData, 0, widthPreview, 0, 0, widthPreview, heightPreview);

            if(CameraStatik.cameraId==CameraInfo.CAMERA_FACING_FRONT)
            {   
                matrix.setRotate(270F);
            }

            finalBitmap = Bitmap.createBitmap(editedBitmap, 0, 0, widthPreview, heightPreview, matrix, true);

            if(saveCurrentFrame)
            {
                saveCurrentFrame =false;

                new BitmapSaver(ctx, finalBitmap).start();
            }

            drawView.setBitmapToDraw(finalBitmap);
        }
    };

我只是希望能够有效地使这个code的工作。

I simply want to make this code work efficiently.

推荐答案

解决它在java方式!使用 执行人 <沿界面code>的Runnable

Solve it in the java way! Use the Executor interface along with Runnable.

首先,获得一个 的ExecutorService (实现执行人接口)与<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newSingleThreadExecutor%28%29\"相对=nofollow> Executors.newSingleThreadExecutor() ,然后,把你的线程工作。

First, get a ExecutorService (which implements the Executor interface) with Executors.newSingleThreadExecutor(), and then, put your Threads to work.

示例:

public YourClass extends Activity {
    // Other methods like onCreate() variable declarations and so on.
    // ...

    private void runOnThread() {
        Executor exe = Executors.newSingleThreadExecutor();
        exe.execute(new Runnable() { 
            public void run() { 
                // Do whatever you want to do.
            } 
        });
    }
    // Other methods...
    // ...
}

您可以阅读更多关于执行人>。

You can read more about Executor here.

或者就像是在评论说,你可以看看的 的ThreadPoolExecutor 和的本教程

Or like it was said on the comments, you can take a look at ThreadPoolExecutor and on this tutorial.

这篇关于如何使线程一系列工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆