要实现的东西是这样:网上有一个java-based的Mpeg解码播发器,源码公开我要做的是针对播放的每一帧去即时的计算其灰度值。
现在,每一帧的灰度值我可以即时计算出来并可视化的在一个Canvas上面话相应的线。最终mpeg放完了,整个Canvas成了一个区域图(类似心电图那种即时生成的东西)。现在的问题是不过要实现同步播放成了老大难问题
只要一算灰度,我这mpeg短片就成相片了。
画线还有点超前,请各位牛牛说该怎么弄?
这个是各帧解码的代码片段
private void copyMacroblock(int motionH, int motionV, int[] Y, int[] Cr, int[] Cb) { //此方法负责计算各帧之间解码。
int V, H;
boolean oddV, oddH;
int dest, scan, last;
//System.out.println("Y size: "+Y.length+"U size: "+Cr.length+"VY size: "+Cb.length);
int value = getDeviation(Y);  //value是此帧平均亮度值
grid.addNewLine(300, value);  //这个就是画线的操作

//System.out.println("codedWidth: "+halfWidth+"codedHeight: "+halfHeight);
}
多谢各位了。

解决方案 »

  1.   


    答:
    1)同步的核心是:对一帧数据,若计算该帧的灰度值耗时 超过 MPEG播放该帧时的耗时,则就没法同步了(除非人为地减慢MPEG播放速度)
    2)假设 计算该帧的灰度值耗时 不超过 MPEG播放该帧时的耗时,则:
    A) 一个线程专门负责播放每一帧(称为:播放线程)
    B)另一个线程专门负责计算该帧的灰度值(称为:计算线程)
    C)两者共用同一个数据结构:ConcurrentLinkedQueue对象.
      时序是:当播放线程开始播放之时,将该帧的数据的引用add(..)放入ConcurrentLinkedQueue对象的末尾.计算线程每一次从ConcurrentLinkedQueue对象的头部remove(..)拿出该帧数据的引用(若:ConcurrentLinkedQueue对象中数据元素为空,则remove(..)拿出时已自动wait()等待了),进行 该帧的灰度值.由于两个线程,都是对同一个数据块(一帧数据)进行并发读操作,这是允许的.以上仅供你参考
      

  2.   

    首先赞同jiangnaisong在这里得理解
    但是对于在该种情况下的计算需要考虑到算法得复杂度。可以考虑将灰度得计算分布到多个线程进行处理,对于使用的线程数量依照下述原则进行线程分工:在实际多线程的处理时,往往线程的交互时延>>线程的处理时延,依照算法复杂度,合并简单处理方法,各线程处理的时间复杂度需要相当。对于在线程同步时使用的交互通道,可以使用ConcurrentLinkedQueue,因为其是线程安全的。但我推荐使用SynchronousQueue。具体使用依据可以去查看下API。
      

  3.   

    我也顶一下!
    刚好遇到类似的问题,我也是再多线程中用ConcurrentLinkedQueue来保证安全,但有个地方要用到他的size(),在API有这样一段话
    需要小心的是,与大多数 collection 不同,size 方法不是 一个固定时间操作。由于这些队列的异步特性,确定当前元素的数量需要遍历这些元素。要取他的长度一定要遍历吗?size()的内部实现也是遍历啊,为什么说要小心,不是和其他collection一样的啊,不是很理解这句话! 
      

  4.   

    攻克学习多线程时碰到的难题     接触多线程已经不少时间了,也做了不少事情,但是一直觉得用起来不那么顺手,在debug的时候,往往会比较担心在同步上出什么问题,想起"程序员最怕的是自己写的代码"这句话,觉得真是不假.
        终于有一天,我觉得是时候把这个问题弄清楚了,所以,我就在网上找相关的内容看,结果竟然是找不到在我这个阶段应该看的,不是太简单,就是一笔带过,不知所云.
        废了九牛二虎之力,终于差不多弄清楚了,其中有不少误区,以前认为的和真理相差甚大.想起自己花费的时间,真是觉得有点多,所以把它写出来,一是防止自己以后又会忘掉,二是给像我一样的似懂非懂者留下一点可以参考的东东.
        闲话少说,转入正题!
      ---------------------------------
        先从线程的创建说起.线程的创建一共有两种形式:
      ---------------------------------
        一种是继承自Thread类.Thread 类是一个具体的类,即不是抽象类,该类封装了线程的行为。要创建一个线程,程序员必须创建一个从 Thread 类导出的新类。程序员通过覆盖 Thread 的 run() 函数来完成有用的工作。用户并不直接调用此函数;而是通过调用 Thread 的 start() 函数,该函数再调用 run()。
        
        例如:
        public class Test extends Thread{
          public Test(){
          }
          public static void main(String args[]){
            Test t1 = new Test();
            Test t2 = new Test();
            t1.start();
            t2.start();
          }
          public void run(){
            //do thread's things
          }
        }
    ----------------------------
        
        另一种是实现Runnable接口,此接口只有一个函数,run(),此函数必须由实现了此接口的类实现。
        
        例如:
        public class Test implements Runnable{
          Thread thread1;
          Thread thread2;
          public Test(){
            thread1 = new Thread(this,"1");
            thread2 = new Thread(this,"2");
          }
          public static void main(String args[]){
            Test t = new Test();
            t.startThreads();
          }
          public void run(){
            //do thread's things
          }
          public void startThreads(){
            thread1.start();
            thread2.start();
          }
        }
        两种创建方式差别不大,第一种因为继承自Thread,只创建了自身对象,第二种还得创建Thread对象.但是当你想继承某一其它类时,你只能用后一种方式.大多数人偏爱后一种的原因大概也在于此吧.
    -------------------------
        下面我们来讲synchronized的4种用法吧:
        1.方法声明时使用,放在范围操作符(public等)之后,返回类型声明(void等)之前.这时,线程获得的是成员锁,即一次只能有一个线程进入该方法,其他线程要想在此时调用该方法,只能排队等候,当前线程(就是在synchronized方法内部的线程)执行完该方法后,别的线程才能进入.
     
          例如:
          public synchronized void synMethod() {
            //方法体
          }
        2.对某一代码块使用,synchronized后跟括号,括号里是变量,这样,一次只有一个线程进入该代码块.此时,线程获得的是成员锁.例如:      public int synMethod(int a1){
            synchronized(a1) {
              //一次只能有一个线程进入
            }
          }
        3.synchronized后面括号里是一对象,此时,线程获得的是对象锁.例如:
      public class MyThread implements Runnable {
        public static void main(String args[]) {
        MyThread mt = new MyThread();
        Thread t1 = new Thread(mt, "t1");
        Thread t2 = new Thread(mt, "t2");
        Thread t3 = new Thread(mt, "t3");
        Thread t4 = new Thread(mt, "t4");
        Thread t5 = new Thread(mt, "t5");
        Thread t6 = new Thread(mt, "t6");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
      }
      public void run() {
        synchronized (this) {
          System.out.println(Thread.currentThread().getName());
        }
      }

     
        对于3,如果线程进入,则得到当前对象锁,那么别的线程在该类所有对象上的任何操作都不能进行.在对象级使用锁通常是一种比较粗糙的方法。为什么要将整个对象都上锁,而不允许其他线程短暂地使用对象中其他同步方法来访问共享资源?如果一个对象拥有多个资源,就不需要只为了让一个线程使用其中一部分资源,就将所有线程都锁在外面。由于每个对象都有锁,可以如下所示使用虚拟对象来上锁:  class FineGrainLock {
       MyMemberClass x, y;
       Object xlock = new Object(), ylock = new Object();
       public void foo() {
          synchronized(xlock) {
             //access x here
          }
          //do something here - but don't use shared resources
          synchronized(ylock) {
             //access y here
          }
       }
       public void bar() {
          synchronized(this) {
             //access both x and y here
          }
          //do something here - but don't use shared resources
       }
      }
        4.synchronized后面括号里是类,此时,线程获得的是对象锁.例如:
      class ArrayWithLockOrder{
      private static long num_locks = 0;
      private long lock_order;
      private int[] arr;
      public ArrayWithLockOrder(int[] a)
      {
        arr = a;
        synchronized(ArrayWithLockOrder.class) {//-----这里
          num_locks++;             // 锁数加 1。
          lock_order = num_locks;  // 为此对象实例设置唯一的 lock_order。
        }
      }
      public long lockOrder()
      {
        return lock_order;
      }
      public int[] array()
      {
        return arr;
      }
      }
      class SomeClass implements Runnable
     {
      public int sumArrays(ArrayWithLockOrder a1,
                           ArrayWithLockOrder a2)
      {
        int value = 0;
        ArrayWithLockOrder first = a1;       // 保留数组引用的一个
        ArrayWithLockOrder last = a2;        // 本地副本。
        int size = a1.array().length;
        if (size == a2.array().length)
        {
          if (a1.lockOrder() > a2.lockOrder())  // 确定并设置对象的锁定
          {                                     // 顺序。
            first = a2;
            last = a1;
          }
          synchronized(first) {              // 按正确的顺序锁定对象。
            synchronized(last) {
              int[] arr1 = a1.array();
              int[] arr2 = a2.array();
              for (int i=0; i<size; i++)
                value += arr1[i] + arr2[i];
            }
          }
        }
        return value;
      }
      public void run() {
        //...
      }
      }
        对于4,如果线程进入,则线程在该类中所有操作不能进行,包括静态变量和静态方法,实际上,对于含有静态方法和静态变量的代码块的同步,我们通常用4来加锁.
      -----------------------------
      下面谈一谈一些常用的方法:
      wait(),wait(long),notify(),notifyAll()等方法是当前类的实例方法,
        
            wait()是使持有对象锁的线程释放锁;
            wait(long)是使持有对象锁的线程释放锁时间为long(毫秒)后,再次获得锁,wait()和wait(0)等价;
            notify()是唤醒一个正在等待该对象锁的线程,如果等待的线程不止一个,那么被唤醒的线程由jvm确定;
            notifyAll是唤醒所有正在等待该对象锁的线程.
            在这里我也重申一下,我们应该优先使用notifyAll()方法,因为唤醒所有线程比唤醒一个线程更容易让jvm找到最适合被唤醒的线程.
        对于上述方法,只有在当前线程中才能使用,否则报运行时错误java.lang.IllegalMonitorStateException: current thread not owner.
      --------------------------
        下面,我谈一下synchronized和wait()、notify()等的关系:
        其实用生产者/消费者这个例子最好说明他们之间的关系了:
        public class test {
      public static void main(String args[]) {
        Semaphore s = new Semaphore(1);
        Thread t1 = new Thread(s, "producer1");
        Thread t2 = new Thread(s, "producer2");
        Thread t3 = new Thread(s, "producer3");
        Thread t4 = new Thread(s, "consumer1");
        Thread t5 = new Thread(s, "consumer2");
        Thread t6 = new Thread(s, "consumer3");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
      }
      }
      class Semaphore
         implements Runnable {
      private int count;
      public Semaphore(int n) {
        this.count = n;
      }
      public synchronized void acquire() {
        while (count == 0) {
          try {
            wait();
          }
          catch (InterruptedException e) {
            //keep trying
          }
        }
        count--;
      }
      public synchronized void release() {
        while (count == 10) {
          try {
            wait();
          }
          catch (InterruptedException e) {
            //keep trying
          }
        }
        count++;
        notifyAll(); //alert a thread that's blocking on this semaphore
      }
      public void run() {
        while (true) {
          if (Thread.currentThread().getName().substring(0,8).equalsIgnoreCase("consumer")) {
            acquire();
          }
          else if (Thread.currentThread().getName().substring(0,8).equalsIgnoreCase("producer")) {
            release();
          }
          System.out.println(Thread.currentThread().getName() + " " + count);
        }
        }
      }
           生产者生产,消费者消费,一般没有冲突,但当库存为0时,消费者要消费是不行的,但当库存为上限(这里是10)时,生产者也不能生产.请好好研读上面的程序,你一定会比以前进步很多.
          上面的代码说明了synchronized和wait,notify没有绝对的关系,在synchronized声明的方法、代码块中,你完全可以不用wait,notify等方法,但是,如果当线程对某一资源存在某种争用的情况下,你必须适时得将线程放入等待或者唤醒.
    文章终于写完了,基本上将我学习所得全部写出来了,不过也留下些许遗憾,比如synchronized后是类时的深入的说明及讨论.
      

  5.   

    但是对于在该种情况下的计算需要考虑到算法得复杂度。可以考虑将灰度得计算分布到多个线程进行处理,对于使用的线程数量依照下述原则进行线程分工:在实际多线程的处理时,往往线程的交互时延>>线程的处理时延,依照算法复杂度,合并简单处理方法,各线程处理的时间复杂度需要相当。 对于在线程同步时使用的交互通道,可以使用ConcurrentLinkedQueue,因为其是线程安全的。但我推荐使用SynchronousQueue。具体使用依据可以去查看下API。