🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Java / multi thread kullanımı(basit thread)

 

1-) Java - multi thread kullanımı(basit thread)

 

1-)jFRAME OLUŞTUR VE 1 BUTTON 2 LABEL KOY ww

import javafx.application.Platform;

 

public class NewJFrame extends javax.swing.JFrame {

class ThreadDemo extends Thread {

   private Thread t;

   private String threadName;

   

   ThreadDemo( String name){

       threadName = name;

       System.out.println("Creating " +  threadName );

   }

   public void calis1() throws InterruptedException{

      Platform.runLater(() -> guncelleme.setText("HAHA"));

          Thread.sleep(2000);

         Platform.runLater(() -> guncelleme.setText(""));

   }

    public void calis2() throws InterruptedException{

    Platform.runLater(() -> guncelleme.setText("RASD"));

          Thread.sleep(4000);

          Platform.runLater(() -> guncelleme.setText(""));

   }

   public void run() {

      System.out.println("Running " +  threadName );

      try {

          if(threadName=="calis1"){

              calis1();

          }else if(threadName=="calis2"){

              calis2();

          }

     } catch (InterruptedException e) {

         System.out.println("Thread " +  threadName + " interrupted.");

     }

     System.out.println("Thread " +  threadName + " exiting.");

   }

   

   public void start ()

   {

      System.out.println("Starting " +  threadName );

      if (t == null)

      {

         t = new Thread (this, threadName);

         t.start ();

       

      }

   }

 

}

    public NewJFrame() {

        initComponents();

    }

2-)BUTTONA TIKLAYINCA

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

       ThreadDemo T1 = new ThreadDemo( "calis1");

      T1.start();

      

      ThreadDemo T2 = new ThreadDemo( "calis2");

      T2.start();

    }  

 2021 Ocak 18 Pazartesi
 410