Flashcards
Thread
Cards in this deck (7)
- 1How to create a thread, using Thread and execute?public class MyThread extends Thread{
public void run()
{
//code de be executed
}
}
Creation :
MyThread t = new MyThread();
t.start(); - 2How to create a thread, using Runnable and execute?public class MyRunnable implements Runnable{
public void run()
{
//code de be executed
public void run()
{
}
}
Creation :
MyRunnable r = new MyRunnable();
Thread t = Thread(r)
t.start(); - 3How to create a thread a Deamon Thread?public class MyThread extends Thread{
public void run()
{
//code de be executed
}
}
Creation :
MyThread t = new MyThread();
t.setDeamon(true);
t.start(); - 4How to create a pipe and read it?Creation : Pipe p=Pipe.open();
Read :
Pipe.SinkChannel buf = pipe.source();
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = intChannel.read(buf); - 5How to create a pipe and write in it?Creation : Pipe p=Pipe.open();
Write : Pipe.SinkChannel buf = pipe.sink();
ByteBugger buf = ByteBufer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining())
{
sinkChannel.write(buf);
} - 6A quoi sert un Synchronized method?A réserver un objet clé, fonction appelable qu'une fois que la clé est libre
public synchronized void show(){ } - 7comment declarer un Synchronized bloc?il faut fournir en argument l’objet dont la clé du verrou est nécessaire pour y pénétrer.
synchronized(this) {
}