Java 无法在android中恢复我的线程
我知道这里有很多关于这个主题的问题和答案,但是我无法解决下面的问题来恢复我应用程序中的一个线程 以下是我的简历:-Java 无法在android中恢复我的线程,java,android,multithreading,runnable,Java,Android,Multithreading,Runnable,我知道这里有很多关于这个主题的问题和答案,但是我无法解决下面的问题来恢复我应用程序中的一个线程 以下是我的简历:- Runnable ViewPagerVisibleScroll= new Runnable() { @Override public void run() { if (i <= mAdapter.getCount() - 1) { verticalViewPager.setCurrentI
Runnable ViewPagerVisibleScroll= new Runnable() {
@Override
public void run() {
if (i <= mAdapter.getCount() - 1) {
verticalViewPager.setCurrentItem(i, true);
handler.postDelayed(ViewPagerVisibleScroll, 3000);
i++;
while (isPaused) {
synchronized (ViewPagerVisibleScroll) {
// wait for resume() to be called
try {
ViewPagerVisibleScroll.wait();
// isPaused = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
};
我的问题是,当我调用pause方法时,线程将
pause()
,但当我调用resume()
时,它将不会恢复。请帮助我解决此问题。首先,我看到以下代码:
public void pause() {
synchronized (ViewPagerVisibleScroll) {
isPaused = true; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
public synchronized void resume() {
synchronized (ViewPagerVisibleScroll) {
isPaused = false;
// notify anybody waiting on "this"
ViewPagerVisibleScroll.notify();
}
}
这是:
while (isPaused)
我可以保证,如果调用方法pause()
,即将isPaused
设置为true
,则在isPaused
为true时,将执行此循环。你应该使用!i暂停
:
while (!isPaused)
i、 e.当不暂停时,做点什么。这只是一个介绍
那么,回到你的问题上来,我建议你下一步做:1). 创建用于同步的
static final
对象:
private static final Object syncObj = new Object();
private static final Object stopSyncObj = new Object();
2) 。将您的runnable作为内部类实现:
public class MyRunnable implements Runnable {
private boolean isPaused;
private boolean isStopped;
public MyRunnable(){
synchronized (stopSyncObj){
isStopped = false;
}
synchronized (syncObj){
isPaused = false;
}
}
@Override
public void run() {
if (i <= mAdapter.getCount() - 1) {
verticalViewPager.setCurrentItem(i, true);
handler.postDelayed(ViewPagerVisibleScroll, 3000);
i++;
//Check thread
boolean isStopped = false;
while (!isStopped) {
synchronized (syncObj) {
boolean isPaused = false;
synchronized (syncObj){
isPaused = this.isPaused;
}
if(isPaused) {
// wait for resume() to be called
try {
syncObj.wait();
// isPaused = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//do somethinghere
}
}
//Check 'stop' flag
synchronized (stopSyncObj){
isStopped = this.isStopped;
}
}
}
}
//This method stops runnable forever
public void stopRunnable(){
synchronized (stopSyncObj){
isStopped = true;
}
}
public void pause(){
synchronized (syncObj){
isPaused = true;
}
}
public void resume(){
synchronized (syncObj){
isPaused = false;
syncObj.notifyAll();
}
}
}
更新的答案对于自动滚动,请使用ScheduledThreadPoolExecutor:
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.schedule(new Runnable() {
@Override
public void run() {
//scroll viewpager
}
}, 3, TimeUnit.SECONDS);
要停止它,请使用:
executor.shutdown();
但这是一个非常奇怪的代码,我不能想象你什么时候想用它。你想做什么?我有自动滑动垂直寻呼机,我想用按钮暂停并恢复它。@HimanshuRathore,你为什么不先说呢?请参阅更新的答案please@HimanshuRathore,您也可以尝试使用此库:@HimanshuRathore,请发布更多信息。哪种变体不起作用?你怎么查的?发布你的代码,当然如果你需要帮助。。。
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.schedule(new Runnable() {
@Override
public void run() {
//scroll viewpager
}
}, 3, TimeUnit.SECONDS);
executor.shutdown();