Programação concorrente

Categories: Trabalhos

0

Java é uma linguagem de programação multithreaded que fazprogramação com threads mais fácil, fornecendo suporte embutidolinguagem para threads. As primitivas de built- in no entanto, como blocosslncronizados, Object. wait 0, e Object. notify são insuficientes para as tarefas de programação. Isso leva os desenvolvedores a implementar as suas próprias instalações de alto nível de sincronização, mas dada a dificuldade de problemas de concorrência, suas implementações podem não ser de qualidade, correcta e eficaz, ou alta. O java 2 Platform, standard Edition versao 5. 2SE 5. 0), que também é conhecida como Tiger, forneceu um novo caminho para multithreading mecanismos original PACE 1 ors () são agora reforçad orn. • Sv. ipe to view trabalhar com thread pacote java. util_conc ação Java. Os com wait () e notify • s e sofisticados para s são parte do um conjunto padrão de utilitários de simultaneidade que acilitarão a tarefa dedesenvolvimento de aplicações multithread e servidores. Além disso,esses padrões irá melhorar a qualidade de tais aplicações. O pacote foidefinido através do Java Community Process como a

JSR 1 66: utilitários de simultaneidade. Tais problemas podem ser superados usando classes de utilitários para controlar o bloqueio, como Mutex, que é outro termo para um bloqueio. um mutex, no entanto, não faz ninho como métodos de sincronização ou blocos. public class Mutex { public void acquire() throws InterruptedException { } public void release() { } saída deste exemplo é como se segue: Entrou no testcase Sincronizar para impedir o acesso a dados compartilhados Create/start the thread Thread Thread-l: Entered Thread Thread-2: Entered

Espere um pouco até que nós estamos ‘feito’ com os dados compartilhados Thread hread-3: Entered Unlock shared data Thread Thread-l: Start critical section, in synchronized block Thread Thread-l: End critica’ section, leave synchronized block Thread Thread-2: Start critical section, in synchronized block Thread Thread-2: End critica’ section, leave synchronized block Thread Thread-3: Start critical section, Thread Thread-3: End critica’ section, leave synchronized block Wait for the threads to complete Testcase completed import java. lang. ; public class ATEST16 { ublic final static int NIJMTHREADS = 3; public static int sharedData = O; public static int sharedData2 O; Qualquer objeto java real ou matriz serviria para sincronização * / / * Nós inventamos um aqui uma vez que temos dois itens exclusivos para sincronizar dados * / / * um neste exemplo simples, eles não estão em um objeto static class theLock extends Object { static public theLock lockOb’ect new thel_ock(); static void main(String argvO) { theThread threads[] = new theThread[NUMTHREADS]; System. out. print(“Entered the testcase
“); System. ut. rint(“Synchronize to prevent access to shared data
synchronized (lockobject) { System. out. print(“Create/start the thread
“); for (int i-o; ++i) ( threads[i] = new theThread(); threads[i]. start(); a bit until we’re idone’ with the shared data
“); try ( Thread. sleepeooo; catch (InterruptedException e) { System. out. print(“sleep ‘nterrupted
“); System. out. print(“Unlock shared data
“); System. out. print(“Wait for the threads to complete
“); for(int izo; i ++i) { threads[i] . jo in(); interrupted
“); System. out. printC’Testcase com leted
“);

Svstem. exit(O); seguinte exemplo de código C # demonstra o uso de Mutex. using System; using System. Threading; public class MutexSample{ static Mutex const int grui 1; gM2; I ERS = 100; static Auto ResetEvent Eventl static AutoResetEvent Event2 static AutoResetEvent Event3 static Auto ResetEvent Event4 new AutoResetEvent(false); = new AutoResetEvent(false); public static void Main(StringO gM1 = new Mutex(true,”MyMutex”); h’ Create Mutext initialOwned, with name of “MyMutex” gM2 = new Mutex(true); h’ Create Mutext initialOwned, with no name. Console.

WnteLine(” – Maln Owns gM1 and gM2″); AutoResetEvent[l evs – new Auto ResetEvent[4]; evs[0] – Eventl; // Event for t 1. evs[l] Eveno; h’ Event for t2. evs[2] Eveno; h’ Event for t3. evs[3] – Event4; Event for t4. MutexSample Thread tl t2 tm = new MutexSample( ); new Thread new ThreadStart(tm. t1Start)); ThreadStart(tm. t2Start)); ne Console. WriteLine(” – Main releases gM2″); gM2. ReleaseMutex( // tl and t4 Will end and signal. WaitHandle. WaitAll(evs); // Waiting until all four threads signal that they are dane. Console. WriteLine(“… MutexSample. cs”); public void tl

Start started, Mutex. WaitAll(Mutex[])”); Mutex[] gMs – new Mutex[2]; grus[0] = grvll; // Create and load an array of Mutex objects for WaitAl gMs[1] gM2; Mutex. WaitAll(gMs); // Waits until both Mutex objects are released. Thread. SIeep(2000); Start finished, Mutex. Wa’tAll(MutexO)”); Eventl. // AutoResetEvent. Set( ) flagging method is done. public void t2Start(){ started, gM1. WaitOne( gM1 . WaitOne( ); // Waits until Mutex gM1 is released. Console. WriteLine(“t2Start finished, gM 1 . WaitOne( Event2. Set( public void t3Start(){ Mutex. WaitAny(Mutex0)”);

Teatro

0

ANTONIO RICARDO VITOR -RA 1043913 VANDERCELIA LEAL- RA 1044351 VANESSA APARECIDA DOS SANTOS- RA 1059812 “BEHAVIORISMO NA EDUCAÇAO” CURITIBA-PR AGOSTO

Read More

Pipa

0

SILVA secretário Exec Serviços Postais: LUC BRASILEIRA DE CORR ADMINISTRAÇÃO Pre DE OLIVEIRA ALESSA Pipa Premium By wnlmirtsanros I anpenp

Read More