为什么使用ReentrantLock可以使用synchronized(this)?(Why use a ReentrantLock if one can use synchronized(this)?)
我正在想知道如果可以使用
synchronized (this)
,并发锁是如此重要。 在下面的虚拟代码中,我可以做:
- 同步整个方法或同步脆弱的区域(synchronized(this){...})
- 或者使用ReentrantLock锁定易受攻击的代码区域。
码:
private final ReentrantLock lock = new ReentrantLock(); private static List<Integer> ints; public Integer getResult(String name) { . . . lock.lock(); try{ if (ints.size()==3) { ints=null; return -9; } for (int x=0; x<ints.size(); x++) { System.out.println("["+name+"] "+x+"/"+ints.size()+". values >>>>"+ints.get(x)); } }finally { lock.unlock(); } return random; }
I'm trying to understand what makes the lock in concurrency so important if one can use
synchronized (this)
. In the dummy code below, I can do either:
- synchronized the entire method or synchronize the vulnerable area (
synchronized(this){...}
)- OR lock the vulnerable code area with a ReentrantLock.
Code:
private final ReentrantLock lock = new ReentrantLock(); private static List<Integer> ints; public Integer getResult(String name) { . . . lock.lock(); try { if (ints.size()==3) { ints=null; return -9; } for (int x=0; x<ints.size(); x++) { System.out.println("["+name+"] "+x+"/"+ints.size()+". values >>>>"+ints.get(x)); } } finally { lock.unlock(); } return random; }
原文:https://stackoverflow.com/questions/11821801
最满意答案
你可以这样做:
IF EXIST %VAR%\NUL ECHO It's a directory
但是,这仅适用于名称中没有空格的目录。 当您在变量上加上引号来处理空格时,它将停止工作。 要处理具有空格的目录,请将文件名转换为8.3格式,如下所示:
FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO It's a directory
%%~si
将%%i
转换为8.3文件名。 要查看可以使用FOR
变量执行的所有其他技巧,请在命令提示符下输入HELP FOR
。(注意 - 上面给出的示例是在批处理文件中工作的格式。要在命令行上工作,请在两个地方替换
%%
与%
)。You can do it like so:
IF EXIST %VAR%\NUL ECHO It's a directory
However, this only works for directories without spaces in their names. When you add quotes round the variable to handle the spaces it will stop working. To handle directories with spaces, convert the filename to short 8.3 format as follows:
FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO It's a directory
The
%%~si
converts%%i
to an 8.3 filename. To see all the other tricks you can perform withFOR
variables enterHELP FOR
at a command prompt.(Note - the example given above is in the format to work in a batch file. To get it work on the command line, replace the
%%
with%
in both places.)
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
你可以这样做: IF EXIST %VAR%\NUL ECHO It's a directory 但是,这仅适用于名称中没有空格的目录。 当您在变量上加上引号来处理空格时,它将停止工作。 要处理具有空格的目录,请将文件名转换为8.3格式,如下所示: FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO It's a directory %%~si将%%i转换为8.3文件名。 要查看可以使用FOR变量执行的所有其他技巧,请在命令提示符下输入HELP FOR 。 (注意 ...
-
如何使用批处理脚本对目录中的每个文件执行某些操作(How to do something to each file in a directory with a batch script)[2023-08-17]
命令行用法: for /f %f in (`dir /b c:\`) do echo %f 批量文件用途: for /f %%f in (`dir /b c:\`) do echo %%f 更新 :如果目录包含名称中包含空格的文件,则需要更改for /f命令正在使用的分隔符。 例如,可以使用管道char。 for /f "delims=|" %%f in ('dir /b c:\') do echo %%f 更新2 :(一年半后原始答案:-))如果目录名称本身在名称中有空格,可以使用usebackq选 ... -
使用批处理脚本在名称以<>开头的目录中查找最新文件(Find latest file in a directory whose name starts with <> using batch script)[2022-01-16]
@echo off setlocal enableextensions disabledelayedexpansion set "lastLog=" for /f "delims=" %%a in ('dir /b /o-d "x:\logsFolder\test-install-*.log" 2^>nul') do ( if not defined lastLog set "lastLog=%%a" ) echo %lastLog% 或者,对于 ... -
另一种方法是在获得第一个文件后使用goto (中断FOR循环): FOR %%F IN (%test_firmware_dir%\*.zip) DO ( set filename=%%F goto tests ) :tests echo "%filename%" 在某些情况下它可以运行得更快(一点点),因为它不必经过整个目录。 An alternative is to use goto (breaks FOR loops) after you have your first file: FOR %%F ...
-
要避免自我递归,请在执行前检查名称: for /R %%f in (*.bat) do if not %%f==%~dpnx0 call "%%f" 和Nick的变体将每个批处理文件的工作目录设置到它自己的目录: for /R %%f in (*.bat) do if not %%f==%~dpnx0 (cd %%~pf & call "%%f") To avoid self-recursion check the name before executing: for /R %%f in (*.bat) ...
-
没关系。 只要本地计算机可以访问脚本,就可以将脚本存储在任何位置。 虽然本地计算机可以直接访问虚拟机文件夹,但使用WinSCP / FTP下载文件似乎太过分了。 所以很有可能,我不明白你的问题。 It doesn't matter. Store the scripts wherever you want, as long as the local machine has an access to them. Though if the local machine has a direct access to ...
-
在批处理脚本目录之外的另一个目录中运行批处理脚本(Run a batch script in another directory than the batch script directory)[2021-12-18]
尝试这样的事情: set "your_dir=path_to_your_directory" pushd %cd% cd %your_dir% run_your_command popd Try something like this: set "your_dir=path_to_your_directory" pushd %cd% cd %your_dir% run_your_command popd -
如何使用批处理脚本转换特定目录中的所有文件(How to convert all files in a specific directory using batch script)[2021-12-31]
您可以使用批处理中的for循环在文件夹中循环所有文件(即使应用过滤器),如下所示: @echo off setlocal EnableDelayedExpansion set /a vid=0 cd /d "your_Folder\Goes here" for %%f in (*.h264) do ( ffmpeg -i "%%~f" -c:v libx264 "!vid!.mp4" set /a vid=!vid!+1 ) 那么这里发生了什么? 第一行用于使程序在正确的目录中运行。 之后,使用您 ...