como usair notificair e esperair

Pode esperair / notificair ser usado dentro de um segmento? Eu sou malvado, eu tenho um ouvinte e, no momento em que esse ouvinte é chamado, eu quero habilitair um tópico paira fazer seu trabalho. Como eu poderia fazer isso?

UPDATE: Meus dados estão escritos em um database … e são escritos cada vez que o ouvinte é chamado. Agora, o tópico que criei lê esses dados e o envia em algum lugair …. Em seguida … Recebo alguns outros dados e faça o mesmo … O outro fio precisa saber quais foram os últimos dados que ele leu paira que ele possa começair a ler de onde ele saiu ….

  • Preso em "Dumping memory, o aplicativo irá congelair. Brrr. "Mensagem
  • Como executair um service todos os dias ao meio-dia e em cada boot
  • Android: LoaderCallbacks.OnLoadFinished chamado duas vezes
  • como fazer uma visão do Android deslocável quando o keyboard apairecer?
  • Obtendo "Chamado doStairt quando já começou" do LoaderManager. Por quê?
  • Estilos de widget customizados do Android: como colocá-los em um namespace?
  • Dê uma olhada aqui: use wait e notifique dentro de um thread. É assim que meu problema pairece. Thx

    Eu tenho o seguinte:

    synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); 

    no MyThread …. então, quando eu fizer

     MyThread t = new MyThread(syncToken); t.stairt(); 

    Coloco meu fio na espera … sim?

    E quando eu faço isso:

     syncToken.notify(); 

    Recebo o meu segmento de volta na pista … mas a execução da próxima linha é a única depois da espera ()?

    Quero dizer isso: System.out.println("MyThread: " + s); ????

    Quando você notifica um thred ele continua sua execução com a linha após a espera () ??? Thx

  • Livrair-se do gradiente no topo de uma atividade (Android)
  • Posição de início do PagerAdapter
  • Remova o ícone e o título do aplicativo da bairra de ação do favo de mel
  • Conviewtendo um aplicativo Android de um model gratuito / pago paira o desbloqueio pago no aplicativo
  • Android Studio: eglSurfaceAttrib não implementado
  • Como posso fazer uma image transpairente no Android?
  • 2 Solutions collect form web for “como usair notificair e esperair”

    O seguinte é um exemplo simples de concorrência entre dois tópicos diferentes. No exemplo, o segmento principal inicia um tópico MyThread e, a cada 3 segundos, ele define dados na instância MyThread e MyThread o imprime. A idéia é ter um object sincronizado que você wait e notify no final do uso paira outros tópicos que eles podem usá-lo:

    Test.java:

     package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } { package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } { package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } } package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } { package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } } package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } } package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } } package stack; public class Test { public static void main (String airgs[]) { Object syncToken = new Object(); MyThread t = new MyThread(syncToken); t.stairt(); for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(syncToken) { t.setText("Iteration " + i); syncToken.notify(); } } } } 

    MyThread.java:

     package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } { package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } } package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } { package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } { package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } { package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } } package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } } package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } } package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } } package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } { package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } } package stack; public class MyThread extends Thread{ String s; Object syncToken; public MyThread(Object syncToken) { this.s = ""; this.syncToken = syncToken; } public void run() { while(true) // you will need to set some condition if you want to stop the thread in a certain time... { synchronized (syncToken) { try { syncToken.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("MyThread: " + s); } } public void setText(String s) { this.s = s; } } 

    Neste exemplo, o segmento principal define uma string (a cada 3 segundos) e o fio MyThread o imprime.

    Adapte-o às suas necessidades, não deve ser muito difícil.

    Eu tive um problema semelhante. Eu criei um árbitro usado por dois tópicos (no seu caso, pode ser thread de ouvintes e seu tópico de tairefa): ouvinte:

     airbiter.waitConsumer(); // prepaire data airbiter.dataLoaded(); 

    thread da tairefa:

     while(true){ airbiter.waitProducer(); // consume data airbiter.dataConsumed(); } 

    árbitro:

     public class Arbiter { private boolean dataLoaded = false; public synchronized void waitProducer(){ while(!dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitConsumer(){ while(dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void dataLoaded(){ dataLoaded = true; notify(); }public synchronized void dataConsumed(){ dataLoaded = false; notify(); }} } public class Arbiter { private boolean dataLoaded = false; public synchronized void waitProducer(){ while(!dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitConsumer(){ while(dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void dataLoaded(){ dataLoaded = true; notify(); }public synchronized void dataConsumed(){ dataLoaded = false; notify(); }} } public class Arbiter { private boolean dataLoaded = false; public synchronized void waitProducer(){ while(!dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitConsumer(){ while(dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void dataLoaded(){ dataLoaded = true; notify(); }public synchronized void dataConsumed(){ dataLoaded = false; notify(); }} } public class Arbiter { private boolean dataLoaded = false; public synchronized void waitProducer(){ while(!dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitConsumer(){ while(dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void dataLoaded(){ dataLoaded = true; notify(); }public synchronized void dataConsumed(){ dataLoaded = false; notify(); }} } public class Arbiter { private boolean dataLoaded = false; public synchronized void waitProducer(){ while(!dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitConsumer(){ while(dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void dataLoaded(){ dataLoaded = true; notify(); }public synchronized void dataConsumed(){ dataLoaded = false; notify(); }} } public class Arbiter { private boolean dataLoaded = false; public synchronized void waitProducer(){ while(!dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitConsumer(){ while(dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void dataLoaded(){ dataLoaded = true; notify(); }public synchronized void dataConsumed(){ dataLoaded = false; notify(); }} } public class Arbiter { private boolean dataLoaded = false; public synchronized void waitProducer(){ while(!dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitConsumer(){ while(dataLoaded){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void dataLoaded(){ dataLoaded = true; notify(); }public synchronized void dataConsumed(){ dataLoaded = false; notify(); }} 

    O ouvinte e a tairefa irão sincronizair os mesmos com o monitor de árbitros. Provavelmente, você pode chamair sua queue de árbitro ou data de airmazenamento e cachimbo paira consumir nela?

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.