Skip to content

Commit

Permalink
Added Threads
Browse files Browse the repository at this point in the history
  • Loading branch information
NMKrastev committed Jul 11, 2022
1 parent 6e186d4 commit 2ec1a03
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 60-TimeTask/60-TimeTask.iml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/out/production/" />
<output url="file://$MODULE_DIR$/out/production" />
<output-test url="file://$MODULE_DIR$/../out/test/60-TimeTask" />
<exclude-output />
<content url="file://$MODULE_DIR$">
Expand Down
13 changes: 13 additions & 0 deletions 61-Threads/61-Threads.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/out/production/" />
<output-test url="file://$MODULE_DIR$/../out/test/61-Threads" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added 61-Threads/out/production/Main.class
Binary file not shown.
Binary file added 61-Threads/out/production/MyThread.class
Binary file not shown.
53 changes: 53 additions & 0 deletions 61-Threads/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
public class Main {
public static void main(String[] args) throws InterruptedException {

/*thread - A thread of execution in a program (kind of like a virtual CPU)
The JVM allows an application to have multiple threads running concurrently
Each thread can execute parts of you code in parallel with the main thread
Each thread has a priority.
Threads with higher priority are executed in preference compared to threads with a lower priority
The Java Virtual Machine continues to execute threads until either of the following occurs
1. The exit method of class Runtime has been called
2. All user threads have died
When a JVM starts up, there is a thread which calls the main method
This thread is called “main”
Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection
JVM terminates itself when all user threads (non-daemon threads) finish their execution
*/

//System.out.println(Thread.activeCount());
//Thread.currentThread().setName("Main");
//System.out.println(Thread.currentThread().getName());

//Thread.currentThread().setPriority(10);
//System.out.println(Thread.currentThread().getPriority());

//System.out.println(Thread.currentThread().isAlive());

/*for (int i = 3; i > 0; i--) {
System.out.println(i);
Thread.sleep(1000);
}
System.out.println("Done!");*/

MyThread threadTwo = new MyThread();
//threadTwo.start();
//threadTwo.run(); - status is false
//System.out.println(threadTwo.isAlive());
//threadTwo.setName("Thread Two");
//System.out.println(threadTwo.getName());
//it will inherit the priority of the thread that created it
//System.out.println(threadTwo.getPriority());

//threadTwo.setPriority(1);

//System.out.println(Thread.activeCount());
threadTwo.setDaemon(true);
System.out.println(threadTwo.isDaemon());
threadTwo.start();

}
}
12 changes: 12 additions & 0 deletions 61-Threads/src/MyThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class MyThread extends Thread {

@Override
public void run() {
if (this.isDaemon()) {
System.out.println("This is a Daemon thread");
} else {
System.out.println("This is user thread");
}
}

}

0 comments on commit 2ec1a03

Please sign in to comment.