我该如何修复这个“不完全同步”的消费者生产者示例(How can I fix this “not quite synchronized” consumer producer example)
我试图熟悉ReentrantLock和ConditionVariable类。 我实现了这个Scala代码(其中没有任何“Scala特定”):
object Conditioned { var pops = 0 var max = 20 abstract class NamedThread extends Thread { def myName = this.toString } class Producer(lock:Lock,condition:Condition,source:ListBuffer[Int]) extends NamedThread { override def run = { var number = max var current = 0 while(current < number) { if(lock.tryLock) { try { current += 1 source += current println("producer added data:"+current) condition.signal } finally { lock.unlock Thread.sleep(100) } } } } } class Consumer(lock:Lock,condition:Condition,source:ListBuffer[Int]) extends NamedThread { override def run = { while(pops < max) { println("awaiting") while(source.isEmpty) condition.await println("consumer try lock") if(lock.tryLock) { try { val data = source(source.size - 1) source -= data println("consumer received data:"+data+" hello from:"+myName) pops += 1 } finally { lock.unlock } } } } } def main(args:Array[String]) = { val lock = new ReentrantLock val condition = lock.newCondition var lb = new collection.mutable.ListBuffer[Int]() val producer = new Producer(lock,condition,lb) val consumer = new Consumer(lock,condition,lb) val cons2 = new Consumer(lock,condition,lb) val threads = Array(producer,consumer,cons2) threads.foreach(_.start) threads.foreach(_.join) } }
我试图在ListBuffer中生成20个元素,然后读取它们。 我错过了一些东西,因为我的输出看起来像这样:
awaiting consumer try lock producer added data:1 awaiting consumer try lock consumer received data:1 hello from:Thread[Thread-51,5,trap.exit] awaiting awaiting producer added data:2 producer added data:3 producer added data:4 producer added data:5 producer added data:6 producer added data:7 producer added data:8 producer added data:9 producer added data:10 producer added data:11 producer added data:12 producer added data:13 producer added data:14 producer added data:15 producer added data:16 producer added data:17 producer added data:18 producer added data:19 producer added data:20
但我不知道是什么。 我正在使用一个ConditionVariable,这样我就可以在添加数据时向Consumer发出信号,但它似乎只是第一次运行,然后生产者运行直到它完成。 我需要改变什么才能让它以消费者 - 生产者的方式运行,而不是以机器人工作者的方式运行? :)
I am trying to familiarize myself with the ReentrantLock and ConditionVariable classes. I implemented this Scala code ( without anything "Scala specific" in it ):
object Conditioned { var pops = 0 var max = 20 abstract class NamedThread extends Thread { def myName = this.toString } class Producer(lock:Lock,condition:Condition,source:ListBuffer[Int]) extends NamedThread { override def run = { var number = max var current = 0 while(current < number) { if(lock.tryLock) { try { current += 1 source += current println("producer added data:"+current) condition.signal } finally { lock.unlock Thread.sleep(100) } } } } } class Consumer(lock:Lock,condition:Condition,source:ListBuffer[Int]) extends NamedThread { override def run = { while(pops < max) { println("awaiting") while(source.isEmpty) condition.await println("consumer try lock") if(lock.tryLock) { try { val data = source(source.size - 1) source -= data println("consumer received data:"+data+" hello from:"+myName) pops += 1 } finally { lock.unlock } } } } } def main(args:Array[String]) = { val lock = new ReentrantLock val condition = lock.newCondition var lb = new collection.mutable.ListBuffer[Int]() val producer = new Producer(lock,condition,lb) val consumer = new Consumer(lock,condition,lb) val cons2 = new Consumer(lock,condition,lb) val threads = Array(producer,consumer,cons2) threads.foreach(_.start) threads.foreach(_.join) } }
I am trying to produce 20 elements in a ListBuffer, and then read them. I am missing something, because my output tends to look like this:
awaiting consumer try lock producer added data:1 awaiting consumer try lock consumer received data:1 hello from:Thread[Thread-51,5,trap.exit] awaiting awaiting producer added data:2 producer added data:3 producer added data:4 producer added data:5 producer added data:6 producer added data:7 producer added data:8 producer added data:9 producer added data:10 producer added data:11 producer added data:12 producer added data:13 producer added data:14 producer added data:15 producer added data:16 producer added data:17 producer added data:18 producer added data:19 producer added data:20
But I don't know what. I am using a ConditionVariable so that I can signal the Consumer when data has been added, but it seems it only works the first time , and after that the producer runs until it finishes. What do I need to change to make it run in a consumer-producer fashion, and not in a robot worker kind of way? :)
原文:https://stackoverflow.com/questions/6624175
最满意答案
SimpleMembershipProvider表的架构是固定的,您无法更改它。 您必须编写自定义memberhsip提供程序。
但是如果你需要在你的系统中处理超过2,147,483,647(21亿用户),那么SimpleMembership提供商可能是你需要考虑的最后一件事,作为这个庞大数据量的选项。 因此,如果您要处理这些数据,那么编写自定义成员资格提供程序可能是您可能需要考虑的事情。
The schema for the SimpleMembershipProvider tables is fixed and you cannot change that. You will have to write a custom memberhsip provider.
But if you're gonna need to handle more than 2,147,483,647 (2.1 Billion users) in your system, the SimpleMembership provider is probably the last thing you're gonna have to consider as an option for this huge amount of data. So writing a custom membership provider is probably something you might need to consider anyway if you will be dealing with those kind of figures.
相关问答
更多-
尝试在
节点中的web.config设置以下内容: 架构首先是简单会员?(Schema first and simple membership?)[2022-01-05]
请注意,您已删除了具有连接名称的以下部分。 public UsersContext() // : base("DefaultConnection") // { // } 只需在您的上下文类中添加它。 是的,你可以使用Code first Approach来实现它,我认为它比你现在正在做的容易得多。 Notice , you have removed following part which had the connection name. public UsersContext( ...您是否尝试将用户名字段的排序规则更改为数据库中的区分大小写的变体? ALTER TABLE dbo.MyTable ALTER COLUMN CharCol varchar(10) COLLATE Latin1_General_CS_AS NOT NULL; 您需要调整表名和类型等 Have you tried changing the collation of the username field to a case sensitive variant on the db? ALTER TABLE ...在简单成员资格提供程序中更改UserProfile表的数据类型(Change datatype at UserProfile table in Simple membership provider)[2023-11-26]
SimpleMembershipProvider表的架构是固定的,您无法更改它。 您必须编写自定义memberhsip提供程序。 但是如果你需要在你的系统中处理超过2,147,483,647(21亿用户),那么SimpleMembership提供商可能是你需要考虑的最后一件事,作为这个庞大数据量的选项。 因此,如果您要处理这些数据,那么编写自定义成员资格提供程序可能是您可能需要考虑的事情。 The schema for the SimpleMembershipProvider tables is fixed ...我在这里提供了一个类似问题的答案: 使用简单的成员资格提供程序与mysql这是基于Fabio Costa的一篇文章我在这里找到: http : //fabiocosta.ca/2012/11/24/use-simplemembership-and-oauth- with-any-database-and-datatype / 。 你基本上得到了源代码,根据你的需要更新它(让它编写mysql查询而不是sql)并使用它。 I provided an answer on a similar question he ...简短回答 - 您必须在用户配置文件表中具有int类型的ID列(根据需要命名)。 SimpleMembershipProvider (SMP)在UserProfile表中强制要求两个非空的第一列ID和第二个用户名。 这是当你选择从SimpleMembershipProvider派生时,在你的情况下我怀疑你真的需要。 如果您有任何自定义需求( SimpleMembershipProvider阻止您实现),您可以从MembershipProvider派生。 抛出异常是因为, SimpleMembershipPro ...您可以使用web.config存储成员和角色定义/凭据。 这里有一个示例: http : //www.eggheadcafe.com/articles/20060529.asp In the end, I've decided the best way is to use the SQL database and standard tables.手动创建您的数据库(不要让您的项目自动创建您的数据库),并且当您在选项下创建数据库集归类为Latin1_General_CI_AS .. 要么 如果在数据库上使用土耳其语字符,则数据库归类必须为Turkish_CI_AS,并且可以更改列归类。请按照步骤进行操作。 ( PS:整理是关于排序,如果你使用土耳其字符,如果你想排序像a,b,c,ç,d,e,f,g,h,ı,i,..你的数据库整理必须是土耳其语..) 1)右键单击用户名列,然后单击修改.. 2)点击整理。 3)并选择Latin1_General .. ...1)我发现Simple Membership的UserProfile表部分有点复杂(以一种好的方式),但它对我的应用程序非常有用。 简单成员资格的约定是创建一个名为“UserProfile”的UserProfile表,其中包含两个字段UserId和UserName。 您可以通过修改Filters / InitializeSimpleMembershipAttribute.cs中的WebSecurity.InitializeDatabaseConnection()行来为UserProfile配置不同的表名或不 ...你需要websecurity类http://www.w3schools.com/aspnet/webpages_ref_websecurity.asp you need the websecurity class http://www.w3schools.com/aspnet/webpages_ref_websecurity.asp相关文章
更多- ACTIVEMQ Topic消息 生产者 发布消息后 消费者收不到消息
- [转]So You Want To Be A Producer
- 构建Disruptor实例-生产消费模型完成整个入门示例
- 与synchronized有关的线程同步问题的一点非常规情况
- 一步一步掌握线程机制(三)---synchronized和volatile的使用
- Groovy 文件I/O 操作详解
- Bentley.STAAD.RCDC.V8i.04.01.01.03 1CD
- \ 关于业务逻辑 我是否需要synchronized 方法 困惑?
- 《Oracle 9i数据库管理员指南》PDF
- 《Java 2 图形设计卷I:AWT》(Graphic Java 1.2 Mastering the JFC Volume I:AWT )扫描版[PDF]
最新问答
更多- h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
- 创建一个功能(Create a function)
- 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
- PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
- Qt冻结循环的原因?(Qt freezing cause of the loop?)
- TableView重复youtube-api结果(TableView Repeating youtube-api result)
- 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
- SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
- 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
- 以编程方式创建UITableView(Creating a UITableView Programmatically)
- 如何打破按钮上的生命周期循环(How to break do-while loop on button)
- C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
- 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
- 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
- Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
- 设置kotlin扩展名(Setting a kotlin extension)
- 如何并排放置两个元件?(How to position two elements side by side?)
- 如何在vim中启用python3?(How to enable python3 in vim?)
- 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
- 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
- Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
- dedecms如何安装?
- 在哪儿学计算机最好?
- 学php哪个的书 最好,本人菜鸟
- 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
- 如何覆盖错误堆栈getter(How to override Error stack getter)
- 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
- USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
- 电脑高中毕业学习去哪里培训
- 正则表达式验证SMTP响应(Regex to validate SMTP Responses)