ArrayBlockingQueue始终返回true

时间:2016-07-22 21:16:38

标签: java android multithreading concurrency android-camera

所以我尝试在android中实现一个消费者 - 生产者模式,场景是这样的:

WITH    ProductSales
      AS ( SELECT   p.IDPr ,
                    p.Product ,
                    od.IDST ,
                    ISNULL(SUM(od.Quantity / pu.UnitQty), 0) AS TotalSalesQty
           FROM     dbo.OrdersSales AS oh
                    INNER JOIN dbo.OrdersSalesDetails AS od ON oh.OrdersID = od.OrdersID
                    INNER JOIN dbo.products AS p ON od.IDPr = p.IDPr
                    INNER JOIN dbo.ProductUnits AS pu ON p.IDPr = pu.IDPr
           WHERE    ( oh.OrdersKind = N'مبيعات' )
                    AND ( pu.UnitType = N'رئيسية' )
           GROUP BY p.IDPr ,
                    p.Product ,
                    od.IDST
         ),
    ProductFirstStock
      AS ( SELECT   p.IDPr ,
                    dbo.OrdersPurchasesDetails.IDST ,
                    ISNULL(SUM(dbo.OrdersPurchasesDetails.UserInputQty
                               * p.PurshPrice), 0) AS TotalFirstStocAmount ,
                    ISNULL(SUM(dbo.OrdersPurchasesDetails.UserInputQty), 0) AS TotalFirstStockQty
           FROM     dbo.OrdersPurchases
                    INNER JOIN dbo.OrdersPurchasesDetails ON dbo.OrdersPurchases.OrdersID = dbo.OrdersPurchasesDetails.OrdersID
                    INNER JOIN dbo.products AS p ON dbo.OrdersPurchasesDetails.IDPr = p.IDPr
           WHERE    ( dbo.OrdersPurchases.Kind = N'اول المدة' )
           GROUP BY p.IDPr ,
                    dbo.OrdersPurchasesDetails.IDST
         ),
    ProductPurchases
      AS ( SELECT   p.IDPr ,
                    OrdersPurchasesDetails_3.IDST ,
                    ISNULL(SUM(OrdersPurchasesDetails_3.ItemTotal), 0) AS TotalPurchasesAmount ,
                    ISNULL(SUM(OrdersPurchasesDetails_3.UserInputQty), 0) AS TotalPurchasesQty
           FROM     dbo.OrdersPurchases AS OrdersPurchases_3
                    INNER JOIN dbo.OrdersPurchasesDetails AS OrdersPurchasesDetails_3 ON OrdersPurchases_3.OrdersID = OrdersPurchasesDetails_3.OrdersID
                    INNER JOIN dbo.products AS p ON OrdersPurchasesDetails_3.IDPr = p.IDPr
           WHERE    ( OrdersPurchases_3.Kind = N'مشتريات' )
           GROUP BY p.IDPr ,
                    OrdersPurchasesDetails_3.IDST
         ),
    ProductSalesReturn
      AS ( SELECT   p.IDPr ,
                    od.IDST ,
                    ISNULL(SUM(od.Quantity / pu.UnitQty), 0) AS TotalSalesRetQty
           FROM     dbo.OrdersSales AS oh
                    INNER JOIN dbo.OrdersSalesDetails AS od ON oh.OrdersID = od.OrdersID
                    INNER JOIN dbo.products AS p ON od.IDPr = p.IDPr
                    INNER JOIN dbo.ProductUnits AS pu ON p.IDPr = pu.IDPr
           WHERE    ( oh.OrdersKind = N'مردودات مبيعات' )
                    AND ( pu.UnitType = N'رئيسية' )
           GROUP BY p.IDPr ,
                    od.IDST
         ),
    ProductPurchasesReturn
      AS ( SELECT   p.IDPr ,
                    OrdersPurchasesDetails_2.IDST ,
                    ISNULL(SUM(OrdersPurchasesDetails_2.UserInputQty), 0) AS TotalPurchasesRetQty ,
                    ISNULL(SUM(OrdersPurchasesDetails_2.ItemTotal), 0) AS TotalPurchasesReturnAmount
           FROM     dbo.OrdersPurchases AS OrdersPurchases_2
                    INNER JOIN dbo.OrdersPurchasesDetails AS OrdersPurchasesDetails_2 ON OrdersPurchases_2.OrdersID = OrdersPurchasesDetails_2.OrdersID
                    INNER JOIN dbo.products AS p ON OrdersPurchasesDetails_2.IDPr = p.IDPr
           WHERE    ( OrdersPurchases_2.Kind = N'مردودات مشتريات' )
           GROUP BY p.IDPr ,
                    OrdersPurchasesDetails_2.IDST
         ),
    ProductPurchasesTransfer
      AS ( SELECT   p.IDPr ,
                    OrdersPurchasesDetails_1.IDST ,
                    ISNULL(SUM(OrdersPurchasesDetails_1.UserInputQty), 0) AS TotalPurchasesTransQty
           FROM     dbo.OrdersPurchases AS OrdersPurchases_1
                    INNER JOIN dbo.OrdersPurchasesDetails AS OrdersPurchasesDetails_1 ON OrdersPurchases_1.OrdersID = OrdersPurchasesDetails_1.OrdersID
                    INNER JOIN dbo.products AS p ON OrdersPurchasesDetails_1.IDPr = p.IDPr
           WHERE    ( OrdersPurchases_1.Kind = N'تحويل' )
           GROUP BY p.IDPr ,
                    OrdersPurchasesDetails_1.IDST
         ),
    ProductSalesTransfer
      AS ( SELECT   p.IDPr ,
                    od.IDST ,
                    ISNULL(SUM(od.UserInputQty), 0) AS TotalSalesTransQty
           FROM     dbo.OrdersSales AS oh
                    INNER JOIN dbo.OrdersSalesDetails AS od ON oh.OrdersID = od.OrdersID
                    INNER JOIN dbo.products AS p ON od.IDPr = p.IDPr
           WHERE    ( oh.OrdersKind = N'تحويل' )
           GROUP BY p.IDPr ,
                    od.IDST
         )
SELECT  s.IDST ,
        s.Store,
        p2.IDPr ,
        p2.Product ,
        pu.UnitName ,
        ISNULL(( ISNULL(F.TotalFirstStockQty, 0)
                 + ISNULL(B.TotalPurchasesQty, 0)
                 + ISNULL(RS.TotalSalesRetQty, 0)
                 + ISNULL(PT.TotalPurchasesTransQty, 0) )
               - ( ISNULL(RP.TotalPurchasesRetQty, 0)
                   + ISNULL(A.TotalSalesQty, 0)
                   + ISNULL(ST.TotalSalesTransQty, 0) ), 0) AS Stock ,
        CAST(ISNULL(AVG(( ISNULL(B.TotalPurchasesAmount, 0)
                          + ISNULL(F.TotalFirstStocAmount, 0)
                          - ISNULL(RP.TotalPurchasesReturnAmount, 0) )
                        / NULLIF(ISNULL(B.TotalPurchasesQty, 0)
                                 + ISNULL(F.TotalFirstStockQty, 0)
                                 - ISNULL(RP.TotalPurchasesRetQty, 0), 0)),
                    0) AS DECIMAL(18, 3)) AS AvgPurchasePrice ,
        ISNULL(( ISNULL(F.TotalFirstStockQty, 0)
                 + ISNULL(B.TotalPurchasesQty, 0)
                 + ISNULL(RS.TotalSalesRetQty, 0)
                 + ISNULL(PT.TotalPurchasesTransQty, 0) )
               - ( ISNULL(RP.TotalPurchasesRetQty, 0)
                   + ISNULL(A.TotalSalesQty, 0)
                   + ISNULL(ST.TotalSalesTransQty, 0) ), 0)
        * CAST(ISNULL(AVG(( ISNULL(B.TotalPurchasesAmount, 0)
                            + ISNULL(F.TotalFirstStocAmount, 0)
                            - ISNULL(RP.TotalPurchasesReturnAmount, 0) )
                          / NULLIF(ISNULL(B.TotalPurchasesQty, 0)
                                   + ISNULL(F.TotalFirstStockQty, 0)
                                   - ISNULL(RP.TotalPurchasesRetQty, 0), 0)),
                      0) AS DECIMAL(18, 3)) AS StockValue
FROM    dbo.products AS p2
        LEFT OUTER JOIN ProductSales AS A ON A.IDPr = p2.IDPr
        LEFT OUTER JOIN ProductFirstStock AS F ON p2.IDPr = F.IDPr
        LEFT OUTER JOIN ProductPurchases AS B ON p2.IDPr = B.IDPr
        LEFT OUTER JOIN ProductSalesReturn AS RS ON p2.IDPr = RS.IDPr
        LEFT OUTER JOIN ProductPurchasesReturn AS RP ON p2.IDPr = RP.IDPr
        LEFT OUTER JOIN ProductPurchasesTransfer AS PT ON PT.IDPr = p2.IDPr
        LEFT OUTER JOIN ProductSalesTransfer AS ST ON ST.IDPr = p2.IDPr
        LEFT OUTER JOIN dbo.Stores AS s ON s.IDST = A.IDST
                                           AND s.IDST = B.IDST
                                           AND s.IDST = F.IDST
                                           AND s.IDST = PT.IDST
                                           AND s.IDST = RP.IDST
                                           AND s.IDST = RS.IDST
                                           AND s.IDST = ST.IDST
        LEFT OUTER JOIN dbo.ProductUnits AS pu ON pu.IDPr = p2.IDPr
WHERE   pu.UnitType = N'رئيسية'
GROUP BY F.TotalFirstStockQty ,
        B.TotalPurchasesQty ,
        RS.TotalSalesRetQty ,
        RP.TotalPurchasesRetQty ,
        A.TotalSalesQty ,
        PT.TotalPurchasesTransQty ,
        ST.TotalSalesTransQty ,
        p2.IDPr ,
        p2.Product ,
        s.IDST ,
        s.Store ,
        pu.UnitName
ORDER BY p2.IDPr

但是,当我运行此代码时...它永远不会生成IMAGE_AVOIDED日志语句..它在IMAGE_TAKEN和IMAGE_PUSHED之间交替..一帧被推..并且一帧被处理..

我希望我能够不断地将帧放入mProcessingQueue,当消费者线程准备就绪时,从队列中取出......这样我就可以在队列已满时删除帧并且只放置最新的帧框架进入队列以接近实时..

我确定它是一个同步问题。也许我在一个进程中运行相同的两个线程,因此调度程序希望来回执行任务?

任何想法/指针??

1 个答案:

答案 0 :(得分:0)

所以事实证明上面的实现是正确的..但是我在onPreviewFrame函数调用中做了一些计算..这给了消费者线程足够的时间来执行手头的工作..谢谢大家!