java - Why is the ArrayList not updated in the following example? -
i using jnativehook
library.
an instance of globalkeylistener
starts listening keystrokes , adds them arraylist1
; problem arraylist1
not seem updated (list1itemcount
never bigger 0) when accessing on infinite while(true)
loop in child class. why , must achieve that?
public class child { public static void main(string[] args) { // logger "org.jnativehook" , set level warning. logger logger = logger.getlogger(globalscreen.class.getpackage().getname()); logger.setlevel(level.warning); try { globalscreen.registernativehook(); } catch (nativehookexception ex) { system.err.println("there problem registering native hook."); system.err.println(ex.getmessage()); system.exit(1); } globalkeylistener globalkeylistener = new globalkeylistener(); globalscreen.addnativekeylistener(globalkeylistener); connection conn; while(true){ int list1itemcount = globalkeylistener.arraylist1.size(); if (list1itemcount >= 4) system.out.println(list1itemcount); } } }
globalkeylistener class:
public class globalkeylistener implements nativekeylistener { arraylist<class1> arraylist1 = new arraylist<>(); public arraylist<class1> listing() { return arraylist1; } public void nativekeypressed(nativekeyevent e) { arraylist1.add(new class1(e.getkeycode(), 1)); } public void nativekeyreleased(nativekeyevent e) { arraylist1.add(new class1(e.getkeycode(), 0)); } public void nativekeytyped(nativekeyevent e) { } }
this multithreading issue. keyevent
updates occur on different thread, , while(true)
won't update. also, arraylist
not threadsafe you've written it.
it better synchronize on (the easiest, not best) thing key listener itself, , have wait until update occurs , notify. here's modified code:
globalkeylistener.java
public class globalkeylistener implements nativekeylistener { arraylist<class1> arraylist1 = new arraylist<>(); public arraylist<class1> listing() { return arraylist1; } public void nativekeypressed(nativekeyevent e) { synchronized(this) { arraylist1.add(new class1(e.getkeycode(), 1)); this.notifyall(); } } public void nativekeyreleased(nativekeyevent e) { synchronized(this) { arraylist1.add(new class1(e.getkeycode(), 0)); this.notifyall(); } } public void nativekeytyped(nativekeyevent e) { } }
child.java
public class child { public static void main(string[] args) throws interruptedexception { logger logger = logger.getlogger(globalscreen.class.getpackage().getname()); logger.setlevel(level.warning); try { globalscreen.registernativehook(); } catch (nativehookexception ex) { system.err.println("there problem registering native hook."); system.err.println(ex.getmessage()); system.exit(1); } globalkeylistener globalkeylistener = new globalkeylistener(); globalscreen.addnativekeylistener(globalkeylistener); synchronized(globalkeylistener) { while(true){ int list1itemcount = globalkeylistener.arraylist1.size(); if (list1itemcount >= 4) { system.out.println(list1itemcount); } globalkeylistener.wait(); } } } }
Comments
Post a Comment