Outils pour utilisateurs

Outils du site


lang:java:thread

Ceci est une ancienne révision du document !


Threads

Runnable

public class T implements Runnable
{
  public void run() { }
}
public static void main(String[] args)
{
  Thread t1 = new Thread(new T());
  t1.start();
}

Thread

public class T extends Thread
{
  public void run() { }
}
 
public static void main(String[] args)
{
  T t1 = new T();
  t1.start();
}

Méthodes courantes

  • void start() : éligibilité
  • void run() : instructions du Thread
  • void interrupt() : arrêt programmé
  • boolean interrupted() : thread arrêté ?
  • void stop() : déprécié au profit de interrupt()
  • static void sleep(long ms) : arrêt pendant un certain temps
  • static native Thread currentThread()

Accès concurrent

class C
{
  synchronized void p(){ }
}

est équivalent à

class C
{
  void p()
  {
    synchronized (this){ }
  }
}

Le concept consiste à poser un lock sur une instance ou sur une classe.

lang/java/thread.1569776485.txt.gz · Dernière modification : 2019/09/29 19:01 de root