Skip to content
Snippets Groups Projects
Commit 604692b2 authored by Jonas Maier's avatar Jonas Maier
Browse files

code snippets of U3

parents
No related branches found
No related tags found
No related merge requests found
# Created by https://www.toptal.com/developers/gitignore/api/eclipse,java
# Edit at https://www.toptal.com/developers/gitignore?templates=eclipse,java
### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# PyDev specific (Python IDE for Eclipse)
*.pydevproject
# CDT-specific (C/C++ Development Tooling)
.cproject
# CDT- autotools
.autotools
# Java annotation processor (APT)
.factorypath
# PDT-specific (PHP Development Tools)
.buildpath
# sbteclipse plugin
.target
# Tern plugin
.tern-project
# TeXlipse plugin
.texlipse
# STS (Spring Tool Suite)
.springBeans
# Code Recommenders
.recommenders/
# Annotation Processing
.apt_generated/
.apt_generated_test/
# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet
# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project
### Eclipse Patch ###
# Spring Boot Tooling
.sts4-cache/
### Java ###
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# End of https://www.toptal.com/developers/gitignore/api/eclipse,java
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-15">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ThreadStart</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
public class ThreadStarting {
/*
* shows different ways to create a runnable and pass it to a thread
*/
public static void main(String[] args) {
Thread[] t = new Thread[6];
t[0] = new Thread(new Runnable() {
@Override
public void run() {
doSomething();
}
});
t[1] = new Thread(() -> {
doSomething();
});
t[2] = new Thread(() -> doSomething());
t[3] = new Thread(ThreadStarting::doSomething);
Printer printer = new Printer();
String msg = "Hello, World";
t[4] = new Thread(() -> printer.doSomething(msg));
t[5] = new Thread(printer::doSomethingElse);
for (Thread a : t) {
if (a != null)
a.start();
}
}
static void doSomething() {
System.out.printf("This does something. (%s)\n", Thread.currentThread().getName());
}
}
class Printer {
public void doSomething(String message) {
System.out.printf("%s says %s\n", Thread.currentThread().getName(), message);
}
public void doSomethingElse() {
this.doSomething("Yet another method!");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-15">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ThreadWaitNotify</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
public class Helper {
static void log(String msg) {
System.out.printf("%s>\t%s\n", Thread.currentThread().getName(), msg);
}
static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class SyncMix extends Helper {
public synchronized void synchronizedMethod() {
log("BeginSync");
sleep(1000);
log("EndSync");
}
public void unsynchronizedMethod() {
log("Unsynchronized");
}
/*
* shows that synchronized blocks do not stop unsynchronized methods/blocks from
* executing
*/
public static void main(String[] args) throws InterruptedException {
SyncMix mix = new SyncMix();
Thread a = new Thread(mix::synchronizedMethod);
Thread b = new Thread(mix::unsynchronizedMethod);
Thread c = new Thread(mix::synchronizedMethod);
a.start();
b.start();
c.start();
a.join();
b.join();
c.join();
}
}
\ No newline at end of file
public class WaitNotify extends Helper {
/*
* shows how wait() and notify() interact with each other
*/
public static void main(String[] args) throws InterruptedException {
// lock object which we call wait and notify on
Object lock = new Object();
var notifier = new Thread(new Notifier(lock));
notifier.setName("Notifier");
var waiters = new Thread[2];
for (int i = 0; i < waiters.length; i++) {
waiters[i] = new Thread(new Waiter(lock));
waiters[i].setName("Waiter-" + i);
}
for (var waiter : waiters) {
waiter.start();
}
sleep(1000);
notifier.start();
}
}
/*
* calls .notify on an object.
*/
class Notifier extends Helper implements Runnable {
final Object lock;
public Notifier(Object lock) {
this.lock = lock;
}
public void run() {
synchronized (lock) {
log("Begin");
// Artificial waiting
sleep(1000);
log("PreNotify");
lock.notify();
log("PostNotify");
// Artificial waiting
sleep(1000);
log("End");
}
}
}
/*
* calls .wait on the lock object
*/
class Waiter extends Helper implements Runnable {
final Object lock;
public Waiter(Object lock) {
this.lock = lock;
}
public void run() {
synchronized (lock) {
log("Begin");
while (true) {
try {
log("PreWait");
lock.wait();
log("PostWait");
// Successfully waited
break;
} catch (InterruptedException e) {
// Duh, we got interrupted
// Let's wait again
log("Interrupted");
sleep(1000);
}
}
log("End");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment