如果我想@Qualifier
在构造函数依赖注入上使用注释,我会有以下内容:
public class Example {
private final ComponentExample component;
@Autowired
public Example(@Qualifier("someComponent") ComponentExample component) {
this.component = component;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道lombok注释减少样板代码而不必包含构造函数如下:@RequiredArgsConstructors(onConstructor=@__(@Inject))
但这仅适用于没有限定符的属性.
有人知道是否可以添加限定符@RequiredArgsConstructor(onConstructor = @__(@Autowired))
?
如果我使用该命令,我有一个工作正常的文件lp filename
.
该文件是收据影响打印机的ESC/P文件.这有Linux本机CUPS驱动程序,所有这些都有效.
我试图使用javax.print
API,以便我可以对打印进行更精细的控制,并希望保持跨平台兼容,尽管Linux是目标平台.
我尝试过每一种类型的数据类型的每个DocFlavor的组合人类已知的和(InputStream
,byte[]
,Reader
等)
它要么只是print()
一起忽略命令,要么只是翻出一张白纸.跑步lp filename
打印完美,所以我怎么做javax.print
功能相当于lp filename
?
我没有开始使用javax.print
我可以使用其他"东西"并且可能会开始调查cups4J,但它似乎只会限制我只使用Linux/*nix,这是现在可以的,但宁愿有一个跨平台的解决方案.
我可以lp
在文件上发出系统命令但是,我需要更细粒度的控制.这些不是我们打印的收据,它们是门票,票价从$ 5.00到$数千美元不等.目前,如果我们检测到打印问题,我们将交易无效,如果打印任何内容,其无效,我们不会轻易重印,如果客户丢失了副本,大部分时间都会收取打印新副本的费用.哦,这样做的原因是我们将POS系统从Windows更改为Linux,打印机从串口直接访问到通过USB管理的CUPS.这是我的代码不起作用.任何帮助表示赞赏.
try {
// Find the default service
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// Create the print job
DocPrintJob job = service.createPrintJob();
InputStream in = new FileInputStream("/home/bart/real.escp");
Doc docNew = new SimpleDoc(in,flavor,null);
// Monitor print job events; for the implementation of PrintJobWatcher,
// see Determining …
Run Code Online (Sandbox Code Playgroud) 我有多个线程访问外部资源 - 一个broswer.但是一次只能有一个线程访问它.所以,我正在使用信号量来同步它们.但是,一个线程从GUI获取输入然后访问浏览器以获得结果,应优先于其他线程,我不知道如何使用信号量来实现它.
我认为获取信号量后的每个线程都会检查队列中是否有优先级线程等待,如果是,则将其释放并再次等待.只有优先级线程在获取后才会释放它.
这是一个很好的解决方案还是我可以使用Java API中的其他任何东西?
我有一些明确不是线程安全的代码:
public class ExampleLoader
{
private List<String> strings;
protected List<String> loadStrings()
{
return Arrays.asList("Hello", "World", "Sup");
}
public List<String> getStrings()
{
if (strings == null)
{
strings = loadStrings();
}
return strings;
}
}
Run Code Online (Sandbox Code Playgroud)
getStrings()
预期同时访问的多个线程将被strings
视为null
,因此loadStrings()
(这是一个昂贵的操作)被多次触发.
我想让代码线程安全,作为一个好的世界公民,我首先写了一个失败的Spock规范:
def "getStrings is thread safe"() {
given:
def loader = Spy(ExampleLoader)
def threads = (0..<10).collect { new Thread({ loader.getStrings() })}
when:
threads.each { it.start() }
threads.each { it.join() }
then:
1 * loader.loadStrings()
}
Run Code Online (Sandbox Code Playgroud)
上面的代码创建并启动每个调用的10个线程 …
我正在寻找一种方法来限制线程的数量,这些线程可以使用信号量或类似的方式在Java中运行某些代码段.
我们正在研究类似于Google Guava RateLimiter的东西- 但不是每秒限制数量调用,我们需要限制运行关键代码段的线程数.
需要这个的原因是我们使用的某些库在这里有问题所以我们只是寻找一个快速的解决方法.
这是一个内存数据安全性问题。
Java垃圾收集是否可以安全清除垃圾数据?
显然,在大块数据被垃圾回收之后,我再也无法检索它了,但是黑客仍然可以通过内存转储来检索数据吗?
我需要使用Java服务在AEM中创建一个节点。我创建了一个工作流程,其中一个流程触发了内部的java服务content/dam/Test
。
我需要使用Java服务来创建节点还是仅以编程方式创建节点?
你好,我认为使用 CompletableFuture 和默认设置ForkJoinPool
我可以优化任务的执行而不是经典,ExecutorService
但我错过了一些东西
使用此代码执行需要 1 秒,我有 3 个工作线程:
for (int i = 0; i < 3; i++) {
final int counter = i;
listTasks.add(CompletableFuture.supplyAsync(() -> {
Thread.sleep(1000);
System.out.println("Looking up " + counter + " on thread " + Thread.currentThread().getName());
return null;
}));
}
Run Code Online (Sandbox Code Playgroud)
好吧,看起来很正常。
但是使用此代码,需要 3 秒:
for (int i = 0; i < 9; i++) {
final int counter = i;
listTasks.add(CompletableFuture.supplyAsync(() -> {
Thread.sleep(1000);
System.out.println("Looking up " + counter + " on thread " …
Run Code Online (Sandbox Code Playgroud) java multithreading asynchronous work-stealing completable-future
我刚刚编写了一个简单的java示例来熟悉wait和notify方法的概念.
这个想法是,在调用时notify()
,主线程将打印总和.
MyThread类
public class MyThread extends Thread {
public int times = 0;
@Override
public void run() {
synchronized (this) {
try {
for (int i = 0; i < 10; i++) {
times += 1;
Thread.sleep(500);
if (i == 5) {
this.notify();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
主类
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
synchronized (t) {
t.start();
try { …
Run Code Online (Sandbox Code Playgroud) 这些 Keep-Alive-Timer 线程在左侧有白色标签,下面没有描述白色标签的含义。我想知道这些 Keep-Alive-Timer 线程是如何创建的以及为什么。
实际上,我正在使用Timer
在我的程序中检查心跳消息。每当心跳到来时,我都会安排一个新的TimerTask
,延迟 6 秒。如果没有心跳,TimerTask
则触发并发送警报。