首页 \ 问答 \ 如何在谓词中使用谓词“断言”(How to use the predicate “assert” in a predicate)

如何在谓词中使用谓词“断言”(How to use the predicate “assert” in a predicate)

我有一个问题,我想要一个代码Prolog,因为我将使用谓词“assert”生成新的谓词,但我想使用我的谓词的结果。 我有一个谓词,它计算列表元素的数量就在这里,我必须创建一个新的谓词,在我的知识库中使用谓词“assert”为我添加一个新的谓词,调用谓词“creat_nbStation”使用2“assert”和“compte”作为例子我有creat_nbStation(L)而L是一个Liste它会在我的知识库中添加一个新的谓词它是nb_Station(L,25)使用断言谓词。

感谢您的帮助。 这是我用于计算列表元素数量的谓词,它可以完美地工作

compte([],0).
compte([_|R],N) :- compte(R,N1), N is N1+1, N>0.

I have a problem I want a code Prolog for that I will generate new predicate with the predicate "assert" but I want to use the result of my predicate . i have a predicate that counts the number of element of a list here is it and I have to make a new predicate that will add me a new predicate using the predicate "assert" in my knowledge base calling to the predicate "creat_nbStation" who will use the 2 "assert" and "compte" for exemple i have creat_nbStation(L) and L is a Liste it will add me in my knowledge base a new predicate it's nb_Station(L,25) using assert predicate .

thank you for the help. this is my predicate for counting the number of elements of a list and it worka perfectly

compte([],0).
compte([_|R],N) :- compte(R,N1), N is N1+1, N>0.

原文:https://stackoverflow.com/questions/13694525
更新时间:2024-01-12 08:01

最满意答案

不是线程安全意味着如果多个线程同时尝试访问该对象,则某个访问可能会发生改变,并导致问题。 考虑以下:

int incrementCount() {
    this.count++;
    // ... Do some other stuff
    return this.count;
}

不会是线程安全的。 为什么不是? 设想线程1访问它, count增加,然后进行一些处理。 在通过函数时,另一个线程访问它,再次增加count 。 第一个线程,它从1到2,它现在将返回时从1到3。 线程2也会看到它从1到3,所以2发生了什么?

在这种情况下,你会想要这样的东西(请记住,这不是任何特定于语言的代码,但最接近Java,只有2个我已完成线程之一)

int incrementCount() synchronized {
    this.count++;
    // ... Do some other stuff
    return this.count;
}

这里的synchronized关键字将确保只要有一个线程正在访问它,就不会有其他线程可以访问它。 这意味着线程1会击中它, count从1到2,如预期的那样。 线程2在1处理时击中它,它必须等到线程1完成。 完成后,线程1得到2的返回值,然后线程2返回,并获得预期的3。

现在,一个例子与您在那里的类似,完全是线程安全的,无论如何:

int incrementCount(int count) {
    count++;
    // ... Do some other stuff
    return this.count;
}

由于在这里触及的唯一变量对函数来说是完全局部的,所以不存在同时访问它的两个线程可以尝试处理从另一个线程更改的数据的情况。 这将使它线程安全。

因此,为了回答这个问题,假设函数不修改特定被调用函数之外的任何东西,那么是的,该类可以被认为是线程安全的。


Not being thread safe means that if multiple threads try to access the object at the same time, something might change from one access to the next, and cause issues. Consider the following:

int incrementCount() {
    this.count++;
    // ... Do some other stuff
    return this.count;
}

would not be thread safe. Why is it not? Imagine thread 1 accesses it, count is increased, then some processing occurs. While going through the function, another thread accesses it, increasing count again. The first thread, which had it go from, say, 1 to 2, would now have it go from 1 to 3 when it returns. Thread 2 would see it go from 1 to 3 as well, so what happened to 2?

In this case, you would want something like this (keeping in mind that this isn't any language-specific code, but closest to Java, one of only 2 I've done threading in)

int incrementCount() synchronized {
    this.count++;
    // ... Do some other stuff
    return this.count;
}

The synchronized keyword here would make sure that as long as one thread is accessing it, no other threads could. This would mean that thread 1 hits it, count goes from 1 to 2, as expected. Thread 2 hits it while 1 is processing, it has to wait until thread 1 is done. When it's done, thread 1 gets a return of 2, then thread 2 goes throguh, and gets the expected 3.

Now, an example, similar to what you have there, that would be entirely thread-safe, no matter what:

int incrementCount(int count) {
    count++;
    // ... Do some other stuff
    return this.count;
}

As the only variables being touched here are fully local to the function, there is no case where two threads accessing it at the same time could try working with data changed from the other. This would make it thread safe.

So, to answer the question, assuming that the functions don't modify anything outside of the specific called function, then yes, the class could be deemed to be thread-safe.

相关问答

更多
  • 就像Willem van Rumpt所说的那样,你只是同步了对那个特定属性的访问,而不是它的线程安全。 要不重新发明轮子,可以使用System.Collections.Concurrent( https://msdn.microsoft.com/en-us/library/dd267312 ( v= vs.110).aspx ? cs-save-lang中的集合。 = 1&cs-lang = csharp#code-snippet-1 ) 另一个更可行的选择是使用不可变列表。 不幸的是,afaik .Net ...
  • 这是线程安全的,因为你不会让线程相互踩踏。 在类级锁上进行同步可以保证一次只有一个线程可以访问列表,并且任何一个线程的更改都将对所有其他线程可见,因此hahn的答案是正确的(+1来自我)。 但就问题而言 这是解决并发问题的正确方法,如果不是解决此问题的好方法 答案是,“这取决于具体情况,但总的来说不是。” 每个线程都必须等待同一个锁,当列表被保存时,每个想要添加内容的线程都必须等待。 这可能会产生不可接受的性能问题,具体取决于负载和列表保存的频率。 解决方案取决于具体情况,但通常会尝试最小化保存方法保持锁定 ...
  • 是的,你的班级是线程安全的。 在以下情况下,方法是线程安全 无法访问共享变量 访问共享的不可变变量 访问只能以线程安全方式修改状态的变量(例如通过同步方法) 使用使用相同变量的其他线程的相同锁访问同步块的变量 您的方法无法访问共享变量,因此是线程安全的 Yes your class is thread safe. A method is thread safe if: has no access to shared variables Access to shared immutable variables ...
  • 请注意, initCause()已synchronized , setStackTrace()复制其参数,然后执行一次赋值。 所以Exception实际上似乎是在考虑到线程安全的情况下实现的。 尽管如此,我仍然对任何设计中的例外情况都是在线程之间传递(例如,除了处理非常严重的错误情况之外的任何其他原因)都保持警惕。 它只是感觉不对。 Note that initCause() is synchronized and setStackTrace() copies its parameter and then ...
  • 由于increment方法是static ,它将在ThreadSafeClass的类对象上同步。 decrement方法不是静态的,并且将在用于调用它的实例上同步。 即,它们将在不同的对象上同步,因此两个不同的线程可以同时执行这些方法。 由于++和--操作不是原子的,所以类不是线程安全的。 此外,由于count是static ,因为它是一个同步实例方法的decrement来修改它是不安全的,因为它可以在不同的实例上调用,并且以同样的方式修改count 。 Since the increment method ...
  • 不是线程安全意味着如果多个线程同时尝试访问该对象,则某个访问可能会发生改变,并导致问题。 考虑以下: int incrementCount() { this.count++; // ... Do some other stuff return this.count; } 不会是线程安全的。 为什么不是? 设想线程1访问它, count增加,然后进行一些处理。 在通过函数时,另一个线程访问它,再次增加count 。 第一个线程,它从1到2,它现在将返回时从1到3。 线程2也会看到它从 ...
  • 以下实现线程是否安全? 这取决于您如何在代码中访问和修改MyObject实例,如果您始终使用此MyObject实例作为对象的监视器访问和修改同步块中的MyObject的给定实例,那么它将是线程安全的,否则它赢了是的。 你的put方法不是线程安全的,因为putIfAbsent和replace不是原子地执行的,这可能导致竞争条件问题 ,使用merge(K key, V value, BiFunction remappingFunction)来做完 ...
  • 它是线程安全的,但它不必要的缓慢。 你不需要锁就可以增加一个数字; 相反,你可以使用原子数学。 另外,你正在共享所有实例的锁(它是static ),这是不必要的。 (一次运行两个不同的实例没有任何问题) 最后,(恕我直言)没有一个单独的未初始化状态。 我会这样写: class TransactionIdProvider { private int nextId; public TransactionIdProvider(int id) { nextId = value; ...
  • 那么,它甚至不会编译,因为你试图锁定一个值类型。 引入一个引用类型的单独锁定变量,例如 private static readonly object padlock = new object(); 除此之外: 如果StopBrowserCleaning()在有清理线程时(它正在休眠时)被调用,但是在第一个线程注意到它要关闭之前再次调用StartBrowserCleaning() ,则最终会有两个线程。 你可能想考虑有两个变量 - 一个是“是否意味着清理线程”,另一个是“实际上是否有清理线程”。 另外,如果 ...
  • 关于使类线程安全的“委托”意味着该类包含该类卸载线程安全操作的其他类对象的引用。 考虑, class Delegator { private final AtomicInteger counter = new AtomicInteger(0); public int getNextCounter(){ return counter.incrementAndGet(); } } 这里, Delegator类已将线程安全性委托给其组件counter 。 显然,班级本来可 ...

相关文章

更多

最新问答

更多
  • 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)