W3schools - Thread / Lambda
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
Thread
Allows a program to operate more efficiently by doing multiple things at the same time
Can be used to perform complicated tasks in the background without interrupting the main program
// Be created by extending the Thread
public class Thrd extends Thread{
public void run(){ // overriding run() method
System.put.println(“Thread!”);
}
}
// to implements the Runnable interface
public class Inter implements Runnable{
public void run(){
System.out.println(“Thread”);
}
}
public class Main{
public static void main(String[] args){
// running Thread
Thrd thread1 = new Thrd();
thread1.start();
// running Thread
Inter interface = new Inter();
Thread thread2 = new Thread(interface);
thread.start();
}
}
The major difference is that when a class extends the Thread class, you can’t any other class,
But by implementing the Runnable interface, it is possibel to extend from anoteher class as well
Concurrency Problems
Threads run at the same time as other parts of the program, there is no way to know in which order the code will run
When the threads and main program are reading and writing the same var, the value are unpredictable
The problems that result from this are called concurrency problems
public class Main extends Thread {
public static int amount = 0;
public static void main(String[] args) {
Main thread = new Main();
thread.start();
// Wait for the thread to finish
while(thread.isAlive()) { // to avoid concurrency problems
// use isAlive() method of the thread to check whether the thread has finished running before using any attributes that the thread can change
System.out.println("Waiting...");
}
// Update amount and print its value
System.out.println("Main: " + amount);
amount++;
System.out.println("Main: " + amount);
}
public void run() {
amount++;
}
}
Lambda
Is a short block of code which takes in parameters and returns a value
They do not need a name and they can be implemented right in the body of a method
parameter -> expression
(parameter1, parameter2) -> expression
Expression are limited
They have to immediately return a value, and can’t contain variables, assignments or statements
In order to do more complex operations, a code block can be used with curly braces
(parameter1, parameter2) -> { code block }
If lambda expression needs to return a value, then the code block have a return statement
ex)
// Lambda expressions are usually passed as parameters to a function
myNumbers.forEach((n) -> {System.out.println(n);});
// Lambda expressions can be stored in var if the var’s type is an interface which has only one method
Consumer<Interger> method = (n) -> {System.out.println(n);};
myNumbers.forEach(method);
// Create a method witch takes a lambda expression as a parameter
interface StringFunction {
String run(String str);
}
// In main method
StringFunction ask = (s) -> s + "?";
printFormatted("Hello", ask);
// In Main class
public static void printFormatted(String str, StringFunction format) {
String result = format.run(str);
System.out.println(result);
}