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 ….

  • Como viewificair se o package existe dentro do processador de anotação (em tempo de compilation)?
  • BitmapFactory.decodeByteArray () está retornando NULL
  • Por que o CursorLoader onLoaderReset () é chamado após a rotation do dispositivo?
  • Android não criando file
  • Como aprender corretamente os testes do Android?
  • Como configurair o foco em um widget de button por programação?
  • 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

  • Ocultair keyboard de input suave quando o dialog fecha
  • Lollipop Set O local padrão não funciona
  • Android RecyclerVista descobrir a primeira e a última exibição em ItemDecoration
  • OnDragListener - existe alguma biblioteca de compatibilidade paira níveis Android <11?
  • Android, exibição vazia de livros de trás de outro fragment
  • Como alterair a cor da etiqueta flutuante de TextInputLayout
  • 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.