首页 \ 问答 \ 等待几个线程中的一个完成?(Wait for one of several threads to finish?)

等待几个线程中的一个完成?(Wait for one of several threads to finish?)

我正在实现一个线程池。 每个线程所需的工作是1-10秒的CPU,所以我很高兴有一个传统的工作线程池,或者我很乐意为每个工作单元生成一个新的线程。 没关系。

我想有一些方法让主控制线程知道N个工作线程中的一个何时完成其工作并准备好更多(或者是时候启动另一个)。 我看过pthread_join和pthread_cond_wait。 似乎没有办法等待N中的一个。所以我想主线程有一个变量,它用来进入睡眠并让工人将其唤醒。 如果工人不死,这似乎有效。 但是,如果它们死了,那么在工作者唤醒控制器的时间和它不能处理的时间之间会有一个窗口。

我看过英特尔的TBB,但它看起来比我需要的要复杂得多。

在Microsoft Windows中是否存在PTHREADS中的WaitForMultipleObjects的简单等价物?


I am implementing a thread pool. The work required by each thread is 1-10 seconds of CPU, so I'm happy to have a traditional thread pool with workers, or I'm happy to spawn a new thread for each unit of work. It doesn't matter.

I would like to have some way for the master control thread to know when one of the N worker threads finishes its work and is ready for more (or its time to start another). I've looked at pthread_join and pthread_cond_wait. There doesn't seem to be a way to wait for one of N. So I thought about having the master thread have a variable that it uses to go to sleep and having the workers wake it up. This seems to work if the workers don't die. However, if they die, there's a window between the time that the worker wakes up the controller and the time it dies which I don't want to deal with.

I've looked at Intel's TBB but it looks much more complicated than I need.

Is there a simple equivalent in PTHREADS to WaitForMultipleObjects in Microsoft Windows?


原文:https://stackoverflow.com/questions/4264460
更新时间:2023-06-15 11:06

最满意答案

你超越了手机/平板电脑的容量。 只是尝试以块的形式加载单元格,如下所示:

        public class ReadExcelFile  {

            ArrayList<String> list = new ArrayList<String>();
            Workbook myWorkBook;

            // open but not close, leave it open

            public void OpenExcelFile()
            {

                WorkbookSettings s = null;
                InputStream excelContent = null;
               try {
                    excelContent = this.getClass().getResourceAsStream("/com/product/sopho/lexiko.xls");
                    s = new WorkbookSettings();
                    s.setUseTemporaryFileDuringWrite(true);
                    myWorkbook = Workbook.getWorkbook(excelContent,s);

                } catch(Exception e) {

                } 

                // we leave it open

            }

            // close when you are done

            public void closeExcelFile() {
                if(myWorkBook != null) {
                    myWorkBook.close();
                }
            }

            // Once opened, get chunks calling ExcelKeyList(0,0,1000) .... etc

            private ArrayList<String> ExcelKeyList(int sheet,int startRow, int endRow)
            {
                Sheet sheet = myWorkBook.getSheet(sheet);       

                ExcelMap(sheet);
                ExcelKeyList(sheet);

                list.clear();

                for (int i=startRow; i<endRow; i++)
                {
                    list.add(sheet.getCell(0,i).getContents());
                }
                return list;
             }

             public ArrayList<String> getExcelKeyList()
             {
                 return list;
              }

        }

你必须在手机爆炸之前找出推荐的块大小。


You are surpassing the phone / tablet capacity. Just try to load the cells in chunks, like this:

        public class ReadExcelFile  {

            ArrayList<String> list = new ArrayList<String>();
            Workbook myWorkBook;

            // open but not close, leave it open

            public void OpenExcelFile()
            {

                WorkbookSettings s = null;
                InputStream excelContent = null;
               try {
                    excelContent = this.getClass().getResourceAsStream("/com/product/sopho/lexiko.xls");
                    s = new WorkbookSettings();
                    s.setUseTemporaryFileDuringWrite(true);
                    myWorkbook = Workbook.getWorkbook(excelContent,s);

                } catch(Exception e) {

                } 

                // we leave it open

            }

            // close when you are done

            public void closeExcelFile() {
                if(myWorkBook != null) {
                    myWorkBook.close();
                }
            }

            // Once opened, get chunks calling ExcelKeyList(0,0,1000) .... etc

            private ArrayList<String> ExcelKeyList(int sheet,int startRow, int endRow)
            {
                Sheet sheet = myWorkBook.getSheet(sheet);       

                ExcelMap(sheet);
                ExcelKeyList(sheet);

                list.clear();

                for (int i=startRow; i<endRow; i++)
                {
                    list.add(sheet.getCell(0,i).getContents());
                }
                return list;
             }

             public ArrayList<String> getExcelKeyList()
             {
                 return list;
              }

        }

You have to find out what's the recommended chunk size before the phone explodes.

相关问答

更多
  • 您可以使用PHPExcel保存内存。 主要有两个: 单元缓存,在开发人员文档的4.2.1节中描述, 这允许您减少从文件读取的每个单元的内存开销 Chunking,在读者用户文档的4.3节中描述 这允许您从文件中读取小范围的行和列,而不是整个工作表 You have a few options for saving memory with PHPExcel. The main two are: cell caching, described in section 4.2.1 of the developer ...
  • FileSystemObject库有一个GetExtensionName()函数,可以让生活更轻松: With CreateObject("Scripting.FileSystemObject") strExt = .GetExtensionName(ActiveWorkbook.Path) End With If StrComp(strExt, "xls", vbTextCompare) = 0 Then ' Display error End If 或者,您可以只检查工作簿中的行数: ...
  • 你可以使用这个API: http://www.andykhan.com/jexcelapi/tutorial.html 您可以根据需要获取任何对象的数据。 You can use this api : http://www.andykhan.com/jexcelapi/tutorial.html And you can get data any object what you want.
  • 我认为你需要打开一个线程或异步任务来从服务器下载文件。 在这个线程中,使用android.os.Handler将任何进度/结果通知给主ui线程。 I think you need to open a thread or async task to download files from server. And in this thread ,notify any progress /result to main ui thread using android.os.Handler.
  • 你有什么问题? 你有任何错误吗? 尝试添加一些侦听器以排除问题。 package { import flash.display.Sprite; import flash.events.*; import flash.net.*; public class URLLoaderExample extends Sprite { public function URLLoaderExample() { var loader:URLLoader ...
  • xls文件可以解释为文本文件,作为csv文件(带逗号的文本)。 但XLSX却与众不同。 如果你用.zip替换.xlsx,你会看到(winzip,winrar。atc)它有很多内部文件......它是一个Office Open XML SpreadsheetML文件格式。 https://msdn.microsoft.com/en-us/library/dd922181(v=office.12).aspx 要打开XLSX文件,请尝试另一个连接String,如下所示: Dim cnn As New OleDb. ...
  • 您可能需要在更改单元格的值之前创建单元格。 如果一个单元格没有值,它就“不存在”,所以你需要创建它然后设置它的值。 您可以尝试使用getCell()和mising rowPolicy来尝试获取一个单元格(如果它们当前不是一个单元格),如下所示: myRow.getCell(7, Row.CREATE_NULL_AS_BLANK);//Should create cell if it is currently blank 获得单元后,尝试设置它的值,就像你一直在做的那样。 或者,如果您有一个细胞,请尝试事先 ...
  • 你超越了手机/平板电脑的容量。 只是尝试以块的形式加载单元格,如下所示: public class ReadExcelFile { ArrayList list = new ArrayList(); Workbook myWorkBook; // open but not close, leave it open public void OpenExcelF ...
  • 我在我的Android项目中使用了jExcel,它工作正常。 I used jExcel in my Android project and it worked fine.
  • 对于这个特定的Coursera练习,而不是一般情况,你可以不使用read_excel函数中的整个URL,而只使用'Energy Indicators.xls' energy = pd.read_excel('Energy Indicators.xls',...) For this specific Coursera exercise, and not as a general case, you can use not the whole URL in read_excel function, but j ...

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)