我知道
同步代码块时,指定要将哪个对象的锁用作锁,因此您可以使用某些第三方对象作为此代码段的锁.这使您能够在单个对象中拥有多个用于代码同步的锁.
但是,我不明白需要将参数传递给块.因为我是否传递String的实例并不重要,所以作为同步块的一些随机类的实例作为同步块的工作完全无论传递给块的参数如何.
所以我的问题是,如果无论如何同步块阻止两个线程同时进入临界区.那么为什么需要传递一个论点呢?(我的意思是默认获取某些随机对象的锁定).
我希望我正确地提出了我的问题.
我尝试了以下示例,随机参数是同步块.
public class Launcher {
public static void main(String[] args) {
AccountOperations accOps=new AccountOperations();
Thread lucy=new Thread(accOps,"Lucy");
Thread sam=new Thread(accOps,"Sam");
lucy.start();
sam.start();
}
}
Run Code Online (Sandbox Code Playgroud)
使用非静态同步块:
public class AccountOperations implements Runnable{
private Account account = new Account();
public void run(){
for(int i=0;i<5;i++){
makeWithdrawal(10);
}
}
public void makeWithdrawal(int amount){
String str="asd"
synchronized (str /* pass any non-null object the synchronized block works*/) {
if(account.getAmount()>10){
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
account.withdraw(amount);
System.out.println(Thread.currentThread().getName()+" has withdrawn 10, …Run Code Online (Sandbox Code Playgroud) 我不明白为什么我必须executorService.shutdown()显式调用终止executorService。如果我不调用,shutdown()那么 JVM 不会自行终止。
我的程序有什么问题或者我缺少什么概念?
public class ExecutorServiceExample {
public static class Task1 implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Message from Task1 :"
+ Thread.currentThread().getName());
}
}
public static class Task2 implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Message from Task2 :"
+ Thread.currentThread().getName());
}
}
public static class Task3 implements Runnable …Run Code Online (Sandbox Code Playgroud) 我正在通过Erlang代码.
tes_lib:check_operational(Config)
Run Code Online (Sandbox Code Playgroud)
上面的代码存在于一个名为的模块中Sample.erl.
我是这门语言的新手.我的问题是,我看不到任何包括模块声明tes_lib在Sample.erl.那么,如何来Sample.erl能够调用该函数check_operational使用tes_lib模块?
我认为它应该像Java,我们首先导入类然后调用该函数.
当我使用静态嵌套类时,我没有收到警告The type parameter T is hiding the type T。但是,当我使用非静态嵌套类时,我收到警告。
public class CustomStack<T> {
private class CustomNode<T>{
private T data;
private CustomNode<T> next;
public CustomNode(T data){
this.data = data;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道,为什么当我使用静态嵌套类时没有收到此警告?
对join()的调用保证使当前线程停止执行,直到它加入的线程(换句话说,它调用join()的线程)完成.
但是,在我的程序中,两个线程同时执行.Thread1没有等待Thread2完成执行.
我的计划有什么问题?
public class Launcher1 {
public static void main(String[] args) {
JoinExample runnable=new JoinExample();
Thread thread1=new Thread(runnable);
Thread thread2=new Thread(runnable);
thread1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
thread2.start();
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class JoinExample implements Runnable{
public void run(){
for(int i=0;i<10;i++){
System.out.println("i:"+i+" "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
i:0线程-0
我:1个线程-0
i:0线程-1
i:2个线程-0
我:1个线程-1
我:3个线程-0
我:4个线程-0
我:2个线程-1 …
我使用以下代码(使用python和BeautifulSoup从网页检索链接获取):
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer
http = httplib2.Http()
status, response = http.request('http://www.nytimes.com')
for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
if link.has_attr('href'):
print link['href']
Run Code Online (Sandbox Code Playgroud)
但是,我不明白为什么我收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\EANUAMA\workspace\PatternExtractor\src\SourceCodeExtractor.py", line 13, in <module>
if link.has_attr('href'):
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
BeautifulSoup 3.2.0 Python 2.7
编辑:
我尝试了类似问题的解决方案(如果link.has_attr('href'),则输入类型错误:TypeError:'NoneType'对象不可调用),但它给出了以下错误:
Traceback (most recent call last):
File "C:\Users\EANUAMA\workspace\PatternExtractor\src\SourceCodeExtractor.py", line 12, in <module>
for link in BeautifulSoup(response).find_all('a', href=True):
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud) 我想使用 linuxdiff命令来获得以下输出:
2,4c2,4
Run Code Online (Sandbox Code Playgroud)
我只想知道文件不同的行号。我不想要控制台上的实际行。
例如:
如果我执行以下命令:
diff file1.txt file2.txt
我想要以下输出:
2,4c2,4
我不想要输出:
2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.
Run Code Online (Sandbox Code Playgroud)
我浏览了命令手册diff,但找不到任何可以让我实现我想要的选项。
我在一些 Perl 代码中发现了以下内容:
if (! -e $options{"inPath"}){
$logger->fatal("Directory \'$options{\"inPath\"}\' does not exist.\n");
}
Run Code Online (Sandbox Code Playgroud)
-e声明中的作用是什么if?