-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
class InnerClass { | ||
static class Test{ | ||
int num = 0; | ||
Test(int n){ | ||
num = n; | ||
} | ||
} | ||
} | ||
|
||
class Main{ | ||
public static void main(String[] args){ | ||
InnerClass.Test test1 = new InnerClass.Test(1); | ||
InnerClass.Test test2 = new InnerClass.Test(2); | ||
System.out.println(test1.num); | ||
System.out.println(test2.num); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import java.util.concurrent.BlockingDeque; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
|
||
class QueueManager{ | ||
private LinkedBlockingDeque<Integer> queue; | ||
QueueManager(){ | ||
this.queue = new LinkedBlockingDeque(50); | ||
} | ||
|
||
void put(Integer ele){ | ||
try { | ||
queue.put(ele); | ||
}catch(InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
Integer take(){ | ||
try{ | ||
return queue.take(); | ||
}catch (InterruptedException e){ | ||
Thread.currentThread().interrupt(); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
class Producer extends Thread{ | ||
private QueueManager queue; | ||
Producer(QueueManager q){ | ||
this.queue = q; | ||
} | ||
|
||
public void run(){ | ||
for(int i=0;i<100;i++){ | ||
System.out.println("put " + i); | ||
// try{ | ||
// Thread.sleep(1000); | ||
// }catch (InterruptedException e){ | ||
// Thread.currentThread().interrupt(); | ||
// } | ||
queue.put(i); | ||
} | ||
} | ||
} | ||
|
||
class Consumer extends Thread{ | ||
private QueueManager queue; | ||
|
||
Consumer(QueueManager q){ | ||
this.queue = q; | ||
} | ||
|
||
public void run(){ | ||
for(int i=0;i<100;i++){ | ||
int a = queue.take(); | ||
try{ | ||
Thread.sleep(1000); | ||
}catch (InterruptedException e){ | ||
Thread.currentThread().interrupt(); | ||
} | ||
System.out.println(a); | ||
} | ||
} | ||
} | ||
|
||
class Test{ | ||
public static void main(String[] args){ | ||
QueueManager queue = new QueueManager(); | ||
Producer producer = new Producer(queue); | ||
Consumer consumer = new Consumer(queue); | ||
producer.start(); | ||
consumer.start(); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import java.util.LinkedList; | ||
|
||
public class LockTest { | ||
private int size = 50; | ||
private LinkedList<Integer> list = new LinkedList<>(); | ||
|
||
synchronized public void put(int num){ | ||
if (isFull()){ | ||
try { | ||
wait(); //进入等待状态可以自己激活么 | ||
System.out.println("============="); | ||
}catch (InterruptedException e){ | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
System.out.println("put" + num); | ||
list.add(num); | ||
notifyAll(); | ||
} | ||
|
||
|
||
public boolean isEmpety(){ | ||
return list.isEmpty(); | ||
} | ||
|
||
public boolean isFull(){ | ||
return list.size() == size; | ||
} | ||
|
||
synchronized public void take(){ | ||
while (isEmpety()){ | ||
try { | ||
wait(); | ||
}catch (InterruptedException e){ | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
System.out.println(list.getFirst()); | ||
list.remove(); | ||
notifyAll(); | ||
} | ||
|
||
public static void main(String[] args){ | ||
LockTest lockTest = new LockTest(); | ||
Producer1 producer1 = new Producer1(lockTest); | ||
Consumer1 consumer1 = new Consumer1(lockTest); | ||
consumer1.start(); | ||
producer1.start(); | ||
|
||
} | ||
} | ||
|
||
class Producer1 extends Thread{ | ||
private LockTest lockTest; | ||
Producer1(LockTest lockTest){ | ||
this.lockTest = lockTest; | ||
} | ||
|
||
public void run(){ | ||
for(int i=0;i<100;i++){ | ||
lockTest.put(i); | ||
} | ||
} | ||
} | ||
|
||
|
||
class Consumer1 extends Thread{ | ||
private LockTest lockTest; | ||
Consumer1(LockTest lockTest){ | ||
this.lockTest = lockTest; | ||
} | ||
|
||
public void run(){ | ||
for(int i=0;i<100;i++){ | ||
lockTest.take(); | ||
} | ||
} | ||
} | ||
|