我正在尝试从驱动器中的计算机中的文本文件中读取文本 D:
所以,我用Java写道:
public class Test {
public static void main(String [] args ) throws IOException{
FileReader in= new FileReader("D:\nir");
BufferedReader bin= new BufferedReader(in);
String text = bin.readLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误异常:
Exception in thread "main" java.io.FileNotFoundException: D:ir
(The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at A11.main(A11.java:14)
Run Code Online (Sandbox Code Playgroud)
我不明白有什么问题,因为文件存在,名称是正确的,也许我没有使用正确的语法和命令?
我在ormlite中写了一个查询,如下所示
Where<Advertisement, Integer> where = queryBuilder.where();
where.and(
where.between("latitude", pLatitude - APPOXIMATION_FACTOR,
pLatitude + APPOXIMATION_FACTOR),
where.between("longitude", pLongitude - APPOXIMATION_FACTOR,
pLongitude + APPOXIMATION_FACTOR)
.and().between("width", pWidth - APPOXIMATION_FACTOR,
pWidth + APPOXIMATION_FACTOR),
);
Run Code Online (Sandbox Code Playgroud)
还有一个和这个
for (int iterator = 0; iterator < moduleList.size(); iterator++) {
where.eq("id", moduleList.get(iterator).getmId());
if (iterator != advertisementList.size() - 1){
whereForModuleID.or();
}
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我被困在如何写查询
寻求帮助
我正在尝试为我的项目编写一些"游戏引擎",我正面临线程问题.当我在主流程中创建一个Thread(LoadThread)时,它会一直等到Run(); 在LoadThread结束.
//loading thread
public class LoadThread implements Runnable{
private boolean running = false;
private Thread loader = null;
public LoadThread(/*init data structures*/){
loader = new Thread(this);
}
public void start(){
running = true;
run();
}
synchronized public void run() {
System.out.println(" loading started ");
while(running){
//do some loading, when done, running = false
}
System.out.println(" loading done ");
}
}
//holds data, starts loading
public class SourceGod {
private LoadThread img_loader;
public void startLoading(){
img_loader = new LoadThread(/* some …
Run Code Online (Sandbox Code Playgroud) 我将线程存储在容器中.随着时间的推移,其中一些线程将会运行,其中一些将会死亡.我想要实现的是:自动(或定期)从容器中删除死(停止)线程.做这个的最好方式是什么?
编辑:我将我的线程存储在一个简单的链表中:
LinkedList<ServerThread> threadPool = new LinkedList<ServerThread>();
Run Code Online (Sandbox Code Playgroud)
这个容器必须是动态的,因为随着时间的推移,我必须添加(并明显删除)线程.
EDIT2:这就是我目前管理线程的方式.正如你所看到的,我等待传入的连接,我不知道什么时候会到达,但是当它发生时,我必须在一个新线程中处理它.
while (!interrupted()) {
try {
Socket clientSocket = serverSocket.accept();
if (portNumber == Server.SMTP_PORT_NUMBER) {
threadPool.add(new SMTPThread(clientSocket, db));
threadPool.getLast().setName("SMTP Thread " + ++threadCounter);
} else {
threadPool.add(new POP3Thread(clientSocket, db));
threadPool.getLast().setName("POP3 Thread " + ++threadCounter);
}
threadPool.get(threadPool.size() - 1).start();
} catch (SocketTimeoutException e) {
} catch (IOException ioe) {
}
}
Run Code Online (Sandbox Code Playgroud) 我有一些循环.在这个循环中,我提出了一些请求并获得了响应文本.在循环结束时,由于某种原因我睡眠线程几秒钟并继续迭代.[Vk朋友]中有大约500个对象,因此重复大约500次,但是当它完成我的程序时,使用的内存比启动时多得多.我使用ARC,我不明白为什么循环中分配的内存不会在每次迭代时释放?这是正常的,还是我错了?
for (Friend *friend in [Vk friends]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"log" object:[NSString stringWithFormat:@"Visit %i/%i friend (earn %i coins)", ++count, [Vk friends].count, [UserState coins] - coinsBefore]];
if (friend.helpPoints <= 0) continue;
strData = [NSString stringWithFormat:@"someparams=somevalues¶m1=%@", [Vk authKey]];
data = [strData dataUsingEncoding:NSUTF8StringEncoding];
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://someaddress/somepath?somegetparams=%@", [Vk userId]]]];
request.HTTPMethod = @"POST";
[request setValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.43 Safari/536.11" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"http://blahblah.com" forHTTPHeaderField:@"Origin"];
[request setValue:@"http://blahblah.com" forHTTPHeaderField:@"Referer"];
[request …
Run Code Online (Sandbox Code Playgroud) 根据文档,我得到了一些关于AtomicReference.compareAndSet()方法的问题,它说:
如果当前值==期望值,则以原子方式将值设置为给定的更新值.
据我所知,==
运营商正在比较两个对象的地址,如果是这样,它将如何在这样的例子中起作用
private AtomicReference<AccessStatistics> stats =
new AtomicReference<AccessStatistics>(new AccessStatistics(0, 0));
public void incrementPageCount(boolean wasError) {
AccessStatistics prev, newValue;
do {
prev = stats.get();
int noPages = prev.getNoPages() + 1;
int noErrors = prev.getNoErrors;
if (wasError) {
noErrors++;
}
newValue = new AccessStatistics(noPages, noErrors);
} while (!stats.compareAndSet(prev, newValue));
}
Run Code Online (Sandbox Code Playgroud)
在这段代码片段中,jvm如何知道要在哪些字段中AccessStatistics
进行比较compareAndSet()
?事实上,我只是想知道这个整个策略是如何工作的,因为java根本不允许覆盖==
?谢谢你的评论!
我在Java书中阅读了下面的代码.我明白主类默认继承Thread类,所以currentThread(); 而不是Thread.currentThread(); 也会做这个工作.
但我得不到的是:Thread.currentThread()中的Thread是什么; 或Thread.sleep(); - 类或对象?一个类及其对象可以具有相同的名称吗?
class Demo {
public static void main(String args[]) {
Thread t=Thread.currentThread();
t.setName("My Thread");
System.out.println("Current Thread: "+t);
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
System.out.println(e);
}
}
}
Run Code Online (Sandbox Code Playgroud) public static void main(String args[]) throws Exception {
ConcurrentHashMap<byte[], Integer> dps =
new ConcurrentHashMap<byte[], Integer>();
System.out.println(dps.putIfAbsent("hi".getBytes(), 1));
System.out.println(dps.putIfAbsent("hi".getBytes(), 1));
}
Run Code Online (Sandbox Code Playgroud)
版画
null
null
Run Code Online (Sandbox Code Playgroud)
为什么不在1
第二行打印?我已经阅读了语义putIfAbsent
,它应该保证工作.(注意:这是从大型并发程序中提炼出来的......正如您所看到的,它现在是单线程的.)
它是关于一个例子wait()
,并notify()
在Java并发.我对它的理论知识没有解释我这个代码,我无法解释为什么这会给我一个误解的结果.
所以,这是获得想法的代码:
public class ExampleOne {
public static void main(String[] args) {
Test b = new Test();
b.start();
synchronized(b){
try{
b.wait();
} catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println(b.total);
}
}
}
class Test extends Thread {
int total;
@Override
public void run(){
synchronized(this){
for(int i =0;i<50;i++){
total+=i;
System.out.println("I am here");
}
notify();
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果是: 4950
那么,如何理解这个过程(怎么total
可能是4950)?
我明白,如果我调用wait()
它会停止调用此方法的对象的线程并唤醒它然后另一个线程调用notify()
.此外,synchronized()
块限制线程并且一次只接受一个线程.
所以当线程调用notify()时,它会变为非活动状态,直到其他线程调用wait()?
wait()和notify()如何在此代码中扮演角色?还synchronized()
阻止?
那么,在这段代码中创建了多少个线程?
我很困惑.帮我解决一下.
我有一些像这样的代码:
public class HelloWorld {
public static void main(String[] args){
ThreadB b = new ThreadB();
b.start();
Runnable a = new Runnable(){
public void run(){
System.out.println(Thread.currentThread().getId());
synchronized(b){
try{
System.out.println("Waiting for b to complete...");
b.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Total is: " + b.total);
}
}
};
(new Thread(a)).start();
synchronized(b){
System.out.println(Thread.currentThread().getId());
try{
System.out.println("Waiting for b to complete...");
b.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Total is: " + b.total);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ThreadB类:
class ThreadB extends Thread{
int total;
@Override
public void run(){
synchronized(this){ …
Run Code Online (Sandbox Code Playgroud) java ×9
concurrency ×2
cocoa ×1
for-loop ×1
hashmap ×1
io ×1
macos ×1
object ×1
objective-c ×1
ormlite ×1
synchronized ×1