-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more strings exercises as well as one more advanced OOP one
- Loading branch information
Showing
8 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Loops | ||
|
||
In class we've see |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package threads.sample.sample1; | ||
|
||
public class Mainer { | ||
public static void main(String[] args) { | ||
int num = 10; | ||
|
||
// Create 10 MyThread objects | ||
// They will NOT start to run just yet | ||
MyThread[] threads = new MyThread[num]; | ||
for (int i = 0; i < threads.length; i++) { | ||
threads[i] = new MyThread(); | ||
} | ||
|
||
for(MyThread t : threads) { | ||
t.start(); // we call start() even though we've overwritten "run()" | ||
} | ||
|
||
for(MyThread t : threads) { | ||
// the x.join() method will make the caller thread | ||
// To wait until the thread 'x' will finish its work | ||
try { | ||
t.join(); // Prevents the main thread from running | ||
// until 't' finishes its work | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
System.out.println("Main is done"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package threads.sample.sample1; | ||
|
||
/*** | ||
* Create your own Thread class by extending the java Thread class | ||
* Or implementing the Runnable interface. | ||
* After doing either of these, you have to override the run() method. | ||
* ONLY code inside of the run() method will run on the separate thread | ||
* | ||
* | ||
* For this example we're extending the Thread class | ||
*/ | ||
public class MyThread extends Thread{ | ||
|
||
@Override | ||
public void run() { | ||
String threadName = Thread.currentThread().getName(); | ||
System.out.println("Hi there my name is " + threadName); | ||
|
||
try { | ||
// Pretend to do extensive work for 2 seconds | ||
Thread.sleep(2 * 1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
System.out.println("Thread: " + threadName + " is done"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package threads.sample.sample2; | ||
|
||
public class Mainer { | ||
|
||
public static void main(String[] args) { | ||
|
||
SharedData sharedData = new SharedData(); // only 1 data object created | ||
|
||
|
||
// Create 10 MyThread objects | ||
// They will NOT start to run just yet | ||
MyThread[] threads = new MyThread[2]; | ||
for (int i = 0; i < threads.length; i++) { | ||
threads[i] = new MyThread(sharedData); | ||
} | ||
|
||
for(MyThread t : threads) { | ||
t.start(); // we call start() even though we've overwritten "run()" | ||
} | ||
|
||
for(MyThread t : threads) { | ||
// the x.join() method will make the caller thread | ||
// To wait until the thread 'x' will finish its work | ||
try { | ||
t.join(); // Prevents the main thread from running | ||
// until 't' finishes its work | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
System.out.println("Main is done: " + sharedData.n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package threads.sample.sample2; | ||
|
||
public class MyThread extends Thread { | ||
private SharedData data; | ||
|
||
public MyThread(SharedData data) { | ||
this.data = data; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
for (int i =0; i < 50; i++) { | ||
// Synchronization happens on the OBJECT | ||
synchronized (data) { | ||
// Only 1 thread is allowed inside this chunk of code | ||
// Incrementation is safe | ||
this.data.n++; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package threads.sample.sample2; | ||
|
||
public class SharedData { | ||
// n is public | ||
// and NOT thread safe | ||
|
||
public int n; | ||
|
||
public SharedData() { | ||
this.n = 0; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package threads.sample.sample3; | ||
|
||
// not complete yet | ||
public class Shared { | ||
|
||
|
||
} |