Java Coding
- Chris Speed
- Nov 26, 2022
- 1 min read
NOTE: void run() function will not return a value
Multi-Threading
To pass a values from a thread constructor to the main function.
Create to second function within the thread constructor
public class Foo implements Runnable {
private volatile int value;
@Overridepublic void run() {
value = 2;
}
public int getValue() {
return value;
}
}
In the main function that calls the thread constructor.
Call the start() and join() from the thread constructor, this will execute both together.
Foo foo = new Foo();
Thread thread = new Thread(foo);
thread.start();
thread.join();
int value = foo.getValue();
If Statements
Use the variable.equals for checking values within if statements instead of "==".
if (Password.equals("a"))
For Loops
Iteration of for each loop that will run.
inti=0;
for (Thread t : Thread.getAllStackTraces().keySet()) {
consumer.accept(i, item);
i++;
}
Comments