作者:石隆雅雯79 | 来源:互联网 | 2023-10-17 13:50
抽象方法不是应该被重写后,才能使用吗?
如下面一个简单FileLocking 程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java . io . * ;
import java . nio . channels . * ;
import java . util . concurrent . * ;
class FileLocking
{
public static void main (String [ ] args ) throws Exception
{
FileOutputStream fos = new FileOutputStream ("File.txt") ;
FileLock fl = fos . getChannel ( ) . tryLock ( ) ;
if (fl != null)
{
System . out . println ("The file is locked") ;
TimeUnit . SECONDS . sleep (5) ;
fl . release ( ) ;
System . out . println ("The file is released") ;
}
fos . close ( ) ;
}
} |