我有一个程序,我正在尝试实现多生产者,多消费者设置.我有一个代码,当我有一个消费者和多个生产者时似乎运作良好,但引入多个消费者线程似乎引发了一些奇怪的问题.
这就是我现在所拥有的:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX 10
typedef struct travs {
int id;
int numBags;
int arrTime;
struct travs *next;
} travs;
travs *queue;
//travs *servicing[MAX];
int produced; // The total # of produced in the queue
pthread_mutex_t queue_lock;
//pthread_mutex_t staff_lock;
pthread_cond_t ct, cs;
int CheckIn(){
sleep(1);
if(produced != 0) return 1;
else return 0;
}
void *producerThread(void *args){
travs *traveler = (travs *)args;
// Acquire the mutex
pthread_mutex_lock(&queue_lock);
produced++;
// pthread_cond_signal(&cs);
pthread_cond_wait(&ct, &queue_lock); …
Run Code Online (Sandbox Code Playgroud) 只读文件对于不太可能更改的配置设置可能很有用,但是只写文件又如何呢?由于系统仍然可以执行二进制只写文件,有人可以举例说明在安全性和编程方面这将是一件好事吗?
我有以下情况:
public class A {
@JsonProperty("member")
private int Member;
}
public class B {
private int Member;
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望执行以下操作:
ObjectMapper mapper = new ObjectMapper();
B b = new B(); b.setMember("1");
A a = mapper.converValue(b, A.class);
Run Code Online (Sandbox Code Playgroud)
通常,这会起作用。但是,由于objectMapper
考虑了诸如之类的注释@JsonProperty
,因此得到以下结果:
A.getMember(); // Member = NULL
Run Code Online (Sandbox Code Playgroud)
有一种解决方法,其中所有可能null
归因于此的字段都是手动设置的,即A.setMember(b.getMember());
,但这首先违背了使用的目的,objectMapper
并且可能容易出错。
有没有一种方法可以配置objectMapper
来忽略@JsonProperty
给定类(或全局)的字段?
我有一个如下所示的数据源配置类,DataSource
使用 JOOQ使用单独的bean 进行测试和非测试环境。在我的代码中,我不使用DSLContext.transaction(ctx -> {...}
而是将方法标记为事务性,以便 JOOQ 遵循 Spring 的事务性声明性事务。我正在使用Spring 4.3.7.RELEASE。
我有以下问题:
@Transactional
按预期工作。无论我使用DSLContext
'sstore()
方法多少次,单个方法都是事务性的,并且 aRuntimeException
会触发整个事务的回滚。@Transactional
完全忽略。一个方法不再是事务性的,并且TransactionSynchronizationManager.getResourceMap()
拥有两个单独的值:一个显示到我的连接池(不是事务性的),一个显示TransactionAwareDataSourceProxy
)。
在这种情况下,我希望只有一个类型的资源TransactionAwareDataSourceProxy
可以包装我的 DB CP。
@Transactional
即使在运行时也能按预期正常工作,但 TransactionSynchronizationManager.getResourceMap()
具有以下值:
在这种情况下,我DataSourceTransactionManager
似乎甚至不知道TransactionAwareDataSourceProxy
(很可能是因为我将它传递给 simple DataSource
,而不是代理对象),这似乎完全“跳过”了代理。
我的问题是:我似乎正确的初始配置,但没有工作。提议的“修复”有效,但 IMO 根本不应该起作用(因为事务管理器似乎不知道TransactionAwareDataSourceProxy
)。
这里发生了什么?有没有更干净的方法来解决这个问题?
@Configuration
@EnableTransactionManagement
@RefreshScope
@Slf4j
public class DataSourceConfig {
@Bean
@Primary
public DSLContext dslContext(org.jooq.Configuration configuration) throws SQLException …
Run Code Online (Sandbox Code Playgroud) 我必须向大量网站发出大量(数千)HTTP GET 请求。这很慢,因为某些网站可能没有响应(或需要很长时间才能响应),而其他网站则超时。因为我需要尽可能多的回复,所以设置一个小的超时(3-5 秒)对我不利。
我还没有在 Python 中进行任何类型的多处理或多线程处理,而且我已经阅读文档有一段时间了。这是我到目前为止所拥有的:
import requests
from bs4 import BeautifulSoup
from multiprocessing import Process, Pool
errors = 0
def get_site_content(site):
try :
# start = time.time()
response = requests.get(site, allow_redirects = True, timeout=5)
response.raise_for_status()
content = response.text
except Exception as e:
global errors
errors += 1
return ''
soup = BeautifulSoup(content)
for script in soup(["script", "style"]):
script.extract()
text = soup.get_text()
return text
sites = ["http://www.example.net", ...]
pool = Pool(processes=5)
results = pool.map(get_site_content, sites)
print results
Run Code Online (Sandbox Code Playgroud)
现在,我希望以某种方式加入返回的结果。这允许两种变化:
每个进程都有一个本地列表/队列,其中包含它积累的内容,并与其他队列连接以形成单个结果,其中包含所有站点的所有内容。 …
我有一个小型 ActiveMQ 客户端,它应该只是连接到 AMQ 服务、创建队列并发送消息。我收到以下错误消息:
\n\nFailed to instantiate SLF4J LoggerFactory\nReported exception:\njava.lang.NoClassDefFoundError: org/apache/logging/log4j/spi/AbstractLoggerAdapter\n at java.lang.ClassLoader.defineClassl(Native Method)\n ...\n
Run Code Online (Sandbox Code Playgroud)\n\n我的上有以下罐子classpath
:
log4j-1.2-api-2.6.2.jar\nlog4j-core-2.6.2.jar\nlog4j-slf4j-impl-2.6.2.jar\n\nactivemq-broker-5.13.3.jar\nactivemq-client-5.13.3.jar\nactivemq-console-5.13.3.jar\nactivemq-jaas-5.13.3.jar\nactivemq-kahadb-store-5.13.3.jar\nactivemq-openwire-legacy-5.13.3.jar\nactivemq-protobuf-1.1.jar\nactivemq-spring-5.13.3.jar\nactivemq-web-5.13.3.jar\ngeronimo-j2ee-management_1.1_spec-1.0.1.jar\ngeronimo-jms_1.1_spec-1.1.1.jar\ngeronimo-jta_1.0.1B_spec-1.0.1.jar\nhawtbuf-1.11.jar\njcl-over-slf4j-1.7.13.jar\nslf4j-api-1.7.13.jar\n
Run Code Online (Sandbox Code Playgroud)\n\n如果我去掉前三个库,我会收到错误Failed to load class \xe2\x80\x9corg.slf4j.impl.StaticLoggerBinder\xe2\x80\x9d
我尝试过搜索,但我不能说我找到了迄今为止有效的任何东西。
\n我们有以下场景:
测试中,一些上下文变量需要更新。在测试中的确切位置以及确切应该发生的事情是可变的。我想提供一个“包装器”函数,它设置一些上下文变量,然后执行在函数调用中提供给它的所有断言。
因此,类似于以下内容:
public void withDefaultContextA(Function<???, Void> noArgsCall) {
setupDefaultContextA();
noArgsCall.invoke() // not sure how apply() would be invoked here
}
Run Code Online (Sandbox Code Playgroud)
或者:
public void withContextA(BiFunction<???, Context, Void> providedContextCall) {
setupContext(providedContext); // not sure how apply() would be invoked here
}
Run Code Online (Sandbox Code Playgroud)
在相应的测试中,这些应该被调用如下:
@Test
public void testSomething() {
withDefaultContextA(() -> {
... // do some asserts
}
withContext((new Context(...)) -> {
... // do some asserts
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?可以以这种方式使用 Java 8 函数吗?如果没有,还有另一种方法可以实现这一目标吗?
我有以下几点div
:
<div class="input-group">
<input type="hidden" id="datepicker">
<input type="text" id="input_date" class="form-control" value="" readonly='readonly'>
<span class="input-group-addon" id="input_btn_calendar"><i class="fa fa-calendar"></i></span>
</div>
Run Code Online (Sandbox Code Playgroud)
以及以下 JQuery 调用:
var today = new Date();
$("#input_date").datepicker({
changeYear: false,
numberOfMonths: 1,
altFormat: "yy-mm-dd",
altField: "#datepicker",
minDate: today,
onSelect : function () {
var inputdate = $.datepicker.parseDate('yy-mm-dd', $("#datepicker").val());
}
}).datepicker("setDate",today);
Run Code Online (Sandbox Code Playgroud)
这使得datepicker
当我按下该text
字段时正确显示。但是,我还希望在单击input_btn_calendar
随附跨度内的按钮时显示它。
我试过这个:
$("#input_btn_calendar").click(function() {
$("input_date").datepicker('show')
});
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这没有任何作用。我错过了什么?
我有一小段代码来生成任意二叉搜索树的深度优先搜索.这是我的代码:
public void printByDepth()
{
Queue<BinaryNode<T>> queue = new LinkedList<BinaryNode<T>>();
BinaryNode<T> current = this;
queue.add(current);
while(!queue.isEmpty()){
current = queue.remove();
System.out.println(current.element);
if(current.left != null)
queue.add(current.left);
if(current.right != null) // had an extra semicolon here, fixed
queue.add(current.right);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一种非常标准的队列方法,但由于某种原因,第8行(println(current.element)
)产生了一个NPE.我正在使用的树应该产生以下DF输出:F B G A D I C E H
.我已经在纸上完成了这一点,在遍历整个树之前我永远不会得到current = null或queue.isEmpty()= true(至少在这种情况下),所以我不确定为什么会发生这种情况.没有节点具有空内容.
另外,有趣的是,如果我将while条件更改为while(current != null)
我没有获得NPE但输出为:F B G A D I
,则缺少最后一级的元素.
我确定有一些简单的我想念......有什么提示吗?
编辑:失控分号=(谢谢,罗杰.
我有一个F#任务,我试图计算矩阵的转置.很简单,但我一直得到一个值限制错误,我无法弄清楚为什么.我咨询了许多VR错误问题,但我仍处于黑暗中.这是我的代码:
let transpose = function
| [xs ; ys] ->
let rec transpose_helper = function
| [],[] -> []
| x::xs , y::ys -> [x;y] :: transpose_helper (xs,ys)
| _ -> failwith "The matrix is not a square matrix"
transpose_helper(xs,ys)
| _ -> failwith "Incorrectly formatted input"
transpose ([[1;2;3];[2;3;4]]);;
Run Code Online (Sandbox Code Playgroud)
我认为错误部分是由于空列表,但我没做什么似乎有帮助.任何指针都将非常感激.
编辑:以下版本的代码,工作.谁有人解释为什么?
let transpose zs =
match zs with
| [xs;ys] ->
let rec transpose_helper (xs, ys) =
match (xs,ys) with
| ([],[]) -> []
| (x::xs , y::ys) …
Run Code Online (Sandbox Code Playgroud) 我有以下格式的元组列表:
input_list = [(x1, y1), (x2, y2), ... , (xn, yn)]
Run Code Online (Sandbox Code Playgroud)
我想通过在每个元组的第一个参数上应用一个函数并将其存储在不同的元组列表中,将此列表转换为另一个列表.所以:
new_list = [(func(x), y) for (x, y) in input_list]
Run Code Online (Sandbox Code Playgroud)
问题是,func(x)
可能会返回一个空字符串.在那种情况下,我不希望元组(func(x), y)
进入new_list
.我希望我可以这样做:
new_list = [(func(x), y) for (x, y) in input_list if func(x)]
Run Code Online (Sandbox Code Playgroud)
但这会导致计算量增加一倍,而且效率非常低.我怎么能做到这一点?
我有一个关于在编写Unity脚本时gameObject
继承的用法的问题MonoBehaviour
.
在一些教程中,例如,为了制作缩放健康栏,我们检索健康栏的原始比例,如下所示:
originalScale = gameObject.transform.localScale.x;
Run Code Online (Sandbox Code Playgroud)
我玩弄了一下这一点,并认为,因为我正在改变我正在操纵的物体,我也可以使用this
:
originalScale = this.transform.localScale.x;
Run Code Online (Sandbox Code Playgroud)
在Unity中,这两个是否总是等价的(至少在实现时MonoBehaviour
)?是否只是更常见的使用,gameObject
以便明确我们所指的是什么?
java ×5
python ×2
binary-tree ×1
c ×1
c# ×1
classpath ×1
concurrency ×1
datepicker ×1
f# ×1
filtering ×1
jackson ×1
java-8 ×1
javascript ×1
jooq ×1
jquery ×1
log4j2 ×1
permissions ×1
pthreads ×1
security ×1
slf4j ×1
spring ×1
tuples ×1
unity5 ×1