Changes

Jump to navigation Jump to search
1,976 bytes added ,  10:43, 25 April 2022
m
Line 3: Line 3:  
===With apt===
 
===With apt===
 
  <nowiki>sudo apt-add-repository ppa:webupd8team/java
 
  <nowiki>sudo apt-add-repository ppa:webupd8team/java
                              sudo apt-get update
+
                                sudo apt-get update
                              sudo apt-get install oracle-java8-installer</nowiki>
+
                                sudo apt-get install oracle-java8-installer</nowiki>
    
Also ensure your JAVA_HOME variable has been set to:
 
Also ensure your JAVA_HOME variable has been set to:
Line 527: Line 527:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
== Streams ==
+
==Streams==
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
List<Integers> numbers = List.of(3, 5, 8, 213, 45, 4, 7)
 
List<Integers> numbers = List.of(3, 5, 8, 213, 45, 4, 7)
Line 551: Line 551:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
== Classes and Objects ==
+
==Classes and Objects==
 
<source lang="java">
 
<source lang="java">
 
class Person {
 
class Person {
Line 1,019: Line 1,019:  
private transient int variableName;</source>
 
private transient int variableName;</source>
 
Static fields aren't serialized too.
 
Static fields aren't serialized too.
 +
 +
== Threading ==
 +
<syntaxhighlight lang="java">
 +
class Task1 extends Thread {
 +
    public void run() {
 +
        System.out.print("\nTask1 Started");
 +
        // Do something
 +
        Thread.yield(); // let other thread take over
 +
        System.out.print("\nTask1 Done");
 +
    }
 +
}
 +
 +
class Task2 implements Runnable {
 +
    public void run() {
 +
        System.out.print("\nTask2 Started");
 +
        // Do something
 +
        System.out.print("\nTask2 Done");
 +
    }
 +
}
 +
 +
public class ThreadBasicsRunner {
 +
    public static void main(String[] args) throws InterruptedException {
 +
        Task1 task1 = new Task1();
 +
        task1.setPriority(10);  // min = 1 max = 10
 +
        task1.start();
 +
 +
        Task2 task2 = new Task2();
 +
        Thread task2thread = new Thread(task2);
 +
        task2thread.start();
 +
   
 +
        // wait for task1 to end
 +
        task1.join();
 +
       
 +
        // task3
 +
        System.out.print("\nTask3 Started");
 +
        // Do something
 +
        System.out.print("\nTask3 Done");
 +
    }
 +
}
 +
 +
 +
 +
</syntaxhighlight>
 +
 +
=== Executor service ===
 +
<syntaxhighlight lang="java">
 +
import java.util.concurrent.ExecutorService;
 +
import java.util.concurrent.Executors;
 +
 +
class Task extends Thread{
 +
    public Task(int number){
 +
        this.number = number;
 +
    }
 +
   
 +
    public void run() {
 +
        System.out.print("\nTask" + number + " Started");
 +
 +
        for (i=number*100; i<=number*100+99; i++){
 +
            System.out.print(i + " ");
 +
        }
 +
        System.out.print("\nTask" + number + " Done");
 +
    }
 +
}
 +
 +
public class ExecutorServiceRunner {
 +
    public static void main(String[] args){
 +
        // ExecutorService executorService = Executors.newSingleThreadExecutor();
 +
        ExecutorService executorService = Executors.newFixedThreadPool(2);
 +
        executorService.execute(new Task(1));
 +
        executorService.execute(new Task(2));
 +
        executorService.execute(new Task(3));
 +
        executorService.execute(new Task(4));
 +
        executorService.shutdown()
 +
    }
 +
}
 +
</syntaxhighlight>
    
==Eclipse shortcuts==
 
==Eclipse shortcuts==

Navigation menu