Created
          October 7, 2020 12:11 
        
      - 
      
- 
        Save willianrschuck/a54b13a6a2ad78d20c747eda9112c34b to your computer and use it in GitHub Desktop. 
    Implementação simples de Threads em Java
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package br.edu.ifsul.so2; | |
| public class Main { | |
| public static void main(String[] args) { | |
| // Comente as linhas para testar individualmente | |
| testeRunnable(); | |
| testeThread(); | |
| } | |
| public static void testeRunnable() { | |
| Runnable processoUm = new MeuRunnable("Yes Yes Yes"); | |
| Thread t1 = new Thread(processoUm); // Define o runnable que será chamado pela thread quando ela iniciar | |
| t1.start(); | |
| Runnable processoDois = new MeuRunnable("No No No"); | |
| Thread t2 = new Thread(processoDois); | |
| t2.start(); | |
| } | |
| public static void testeThread() { | |
| // Perceba que não foi preciso setar o runnable | |
| // pois o método run da nossa thread foi sobreescrito | |
| Thread t1 = new MinhaThread("Yes Yes Yes"); | |
| t1.start(); | |
| Thread t2 = new MinhaThread("No No No"); | |
| t2.start(); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package br.edu.ifsul.so2; | |
| /* Classe que pode ser executada por uma thread. | |
| * Isso é possível porque ela implementa Runnable | |
| * e por isso possui o método run() */ | |
| public class MeuRunnable implements Runnable { | |
| private String text; | |
| // Construtor da classe | |
| public MeuRunnable(String text) { | |
| this.text = text; | |
| } | |
| // Implementa o método run que é chamado quando a thread é iniciada | |
| @Override | |
| public void run() { | |
| try { | |
| for (int i = 0; i < 10; i++) { | |
| System.out.println(String.format("%d - %s", i, text)); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public String getText() { | |
| return text; | |
| } | |
| public void setText(String text) { | |
| this.text = text; | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package br.edu.ifsul.so2; | |
| public class MinhaThread extends Thread { | |
| public MinhaThread(String name) { | |
| // Chama o construtor da classe pai Thread que | |
| // recebe como parâmetro o nome da thread | |
| super(name); | |
| } | |
| // A classe Thread também implementa runnable e por isso | |
| // possui o método run "dentro" dela, podemos o sobrescrever | |
| // para mudar o seu comportamento | |
| @Override | |
| public void run() { | |
| try { | |
| for (int i = 0; i < 10; i++) { | |
| System.out.println(String.format("%d - %s", i, getName())); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment