Last active
August 22, 2018 03:02
-
-
Save shannah/2becc8cf4e23b89ef78f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 shannah. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package ca.weblite.comiccl.forms; | |
import com.codename1.io.Log; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
/** | |
* | |
* @author shannah | |
*/ | |
public class ThreadQueue implements Runnable { | |
final private ArrayList<Runnable> queue = new ArrayList<Runnable>(); | |
final Object lock = new Object(); | |
private Thread self; | |
public void setSelf(Thread t){ | |
self = t; | |
} | |
@Override | |
public void run(){ | |
while ( true ){ | |
synchronized (lock){ | |
while (queue.isEmpty()){ | |
try { | |
lock.wait(); | |
} catch (InterruptedException ex) { | |
} | |
} | |
} | |
ArrayList<Runnable> toRun = new ArrayList<Runnable>(); | |
synchronized(lock){ | |
toRun.addAll(queue); | |
queue.clear(); | |
} | |
for ( Runnable r : toRun ){ | |
try { | |
r.run(); | |
} catch ( Throwable t){ | |
Log.e(t); | |
} | |
} | |
try { | |
synchronized(lock){ | |
lock.wait(200); | |
} | |
} catch (InterruptedException ex) { | |
Log.e(ex); | |
} | |
} | |
} | |
public void invokeLater(Runnable r){ | |
synchronized(lock){ | |
queue.add(r); | |
lock.notifyAll(); | |
} | |
} | |
public void invokeIOAndWait(final IORunnable r) throws IOException { | |
if ( Thread.currentThread() == self ){ | |
//Log.p("Running IO "+r+" on same thread"); | |
r.run(); | |
return; | |
} | |
final IOException[] ex = new IOException[1]; | |
invokeAndWait(new Runnable(){ | |
public void run() { | |
//Log.p("Running IO "+r+" on dedicated thread"); | |
try { | |
r.run(); | |
} catch ( IOException e){ | |
ex[0] = e; | |
} | |
} | |
}); | |
if ( ex[0] != null ){ | |
throw ex[0]; | |
} | |
} | |
public void invokeAndWait(final Runnable r){ | |
if ( Thread.currentThread() == self ){ | |
//Log.p("Running "+r+" on same thread"); | |
r.run(); | |
return; | |
} | |
final Object o = new Object(); | |
final boolean[] done = new boolean[1]; | |
this.invokeLater(new Runnable(){ | |
public void run() { | |
//Log.p("Running IO "+r+" on diff thread"); | |
try { | |
r.run(); | |
} finally { | |
synchronized(o){ | |
done[0] = true; | |
o.notify(); | |
} | |
} | |
//Log.p("Finished running "+r+" on diff thread"); | |
} | |
}); | |
synchronized(o){ | |
try { | |
//Log.p("Waiting for "+r); | |
while ( !done[0] ) o.wait(); | |
//Log.p("Finished waiting for "+r); | |
} catch (InterruptedException ex) { | |
//Log.e(ex); | |
} | |
} | |
} | |
public static interface IORunnable { | |
public void run() throws IOException; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Start the queue on some thread: | |
dbThread = new ThreadQueue(); | |
Thread th = Display.getInstance().startThread(dbThread, "DB THREAD"); | |
dbThread.setSelf(th); | |
th.start(); | |
// Call all SQL code inside dbThread.invokeIOAndWait() | |
dbThread.invokeIOAndWait(()->{ | |
daoProvider = new DAOProvider(db, "/setup.sql", DB_VERSION); | |
//Log.p("Setting collections dao"); | |
daoProvider.set("collections", new CollectionDAO(daoProvider)); | |
//Log.p("Setting issues dao"); | |
daoProvider.set("issues", new IssueDAO(daoProvider)); | |
//Log.p("Setting series dao"); | |
daoProvider.set("series", new SeriesDAO(daoProvider)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment