Skip to content

Instantly share code, notes, and snippets.

@kenaiX
Created April 8, 2015 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenaiX/4de5c370e42d25230ceb to your computer and use it in GitHub Desktop.
Save kenaiX/4de5c370e42d25230ceb to your computer and use it in GitHub Desktop.
test for wait()
public class LoadingActivity extends Activity {
final byte[] lock = new byte[0];
final int goal = 3;
volatile int task = 0;
@Override
protected final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final long time = SystemClock.elapsedRealtimeNanos();
new ViewPager(this);
new Toolbar(this);
new ActionMenuView(this);
// synchronized (lock) {
// startWork("TEST");
// try {
// lock.wait();
// } catch (InterruptedException e) {
// }
// }
Log.d("time", "" + (SystemClock.elapsedRealtimeNanos() - time));
}
public void startWork(final String task_info) {
task = 0;
new Thread(new Runnable() {
@Override
public void run() {
new ViewPager(LoadingActivity.this);
task++;
if (task >= goal) {
Log.d(Thread.currentThread().getName(), "finish task " + task_info);
synchronized (lock) {
lock.notify();
}
}
}
}, "worker").start();
new Thread(new Runnable() {
@Override
public void run() {
new Toolbar(LoadingActivity.this);
task++;
if (task >= goal) {
Log.d(Thread.currentThread().getName(), "finish task " + task_info);
synchronized (lock) {
lock.notify();
}
}
}
}, "worker").start();
new Thread(new Runnable() {
@Override
public void run() {
new ActionMenuView(LoadingActivity.this);
task++;
if (task >= goal) {
Log.d(Thread.currentThread().getName(), "finish task " + task_info);
synchronized (lock) {
lock.notify();
}
}
}
}, "worker").start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment