Skip to content

Commit

Permalink
首次提交
Browse files Browse the repository at this point in the history
  • Loading branch information
yaozhichao committed May 13, 2017
0 parents commit e9082a8
Show file tree
Hide file tree
Showing 106 changed files with 4,214 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="F:/JAVA资料/javaConcurrentAnimated.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Solution</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>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding//src/Main.java=UTF-8
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
39 changes: 39 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/BlockingDequeTester.class
/Concurrency.class
/Consumer.class
/ConsumerTest.class
/CreatorAndConsumerBlock/
/Divide.class
/ElegantPoint.class
/FreshJuice$FreshJuiceSize.class
/FreshJuice.class
/FreshJuiceTest.class
/JumpBoard.class
/LargestFileSize.class
/Main.class
/Main1.class
/Main123.class
/Main3.class
/MainTouTiao1.class
/MainTouTiao2.class
/MainTouTiao3.class
/MethodReferenceAndLamda.class
/MyComparator.class
/OddYS.class
/Point.class
/Prime.class
/Producer.class
/Rev.class
/Student.class
/T.class
/Test.class
/TestProducerConsumer$Consumer.class
/TestProducerConsumer$Producer.class
/TestProducerConsumer.class
/TestReflection.class
/com/
/picrec/
/practice/
/synch/
/test/
/unsynch/
69 changes: 69 additions & 0 deletions src/BlockingDequeTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class Producer implements Runnable {
private String name;

private BlockingDeque<Integer> deque;

public Producer(String name, BlockingDeque<Integer> deque) {
this.name = name;
this.deque = deque;
}

public synchronized void run() {

for (int i = 0; i < 10; i++) {
try {
deque.putFirst(i);
System.out.println(name + " puts " + i);
Thread.sleep(300);

} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}

class Consumer implements Runnable {
private String name;

private BlockingDeque<Integer> deque;

public Consumer(String name, BlockingDeque<Integer> deque) {
this.name = name;
this.deque = deque;
}

public synchronized void run() {
for (int i = 0; i < 10; i++) {
try {
int j = deque.takeLast();
System.out.println(name + " takes " + j);
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public class BlockingDequeTester {
public static void main(String[] args) {
BlockingDeque<Integer> deque = new LinkedBlockingDeque<Integer>(5);
Runnable producer0 = new Producer("Producer0", deque);
Runnable producer1 = new Producer("Producer1", deque);
Runnable consumer = new Consumer("Consumer", deque);
new Thread(producer0).start();
new Thread(producer1).start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

new Thread(consumer).start();
}
}
56 changes: 56 additions & 0 deletions src/Concurrency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class Producer implements Runnable {
private final BlockingQueue queue;
Producer(BlockingQueue q) {
queue = q;
}

public void run() {
try {
while (true) {
queue.put(produce());
}
} catch (InterruptedException ex) {
System.out.println("Producer interrupted");
}
}

Object produce() {
return new Object();
}
}

class ConsumerTest implements Runnable {
public final BlockingQueue queue;
public ConsumerTest(BlockingQueue q) {
queue = q;
}

public void run() {
try {
while (true) {
consume(queue.take());
}
} catch (InterruptedException ex) {
System.out.println("Consumer interrupted");
}
}

void consume(Object x) {

}
}

public class Concurrency {
public static void main(String[] args) {
BlockingQueue q = new ArrayBlockingQueue(10);
Producer p = new Producer(q);
ConsumerTest c1 = new ConsumerTest(q);
ConsumerTest c2 = new ConsumerTest(q);
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
}
}
18 changes: 18 additions & 0 deletions src/CreatorAndConsumerBlock/Consumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package CreatorAndConsumerBlock;


public class Consumer implements Runnable {
/**
* 线程资源
*/
private Plate plate;

public Consumer(Plate plate) {
this.plate = plate;
}

@Override
public void run() {
plate.getEgg();
}
}
23 changes: 23 additions & 0 deletions src/CreatorAndConsumerBlock/Creator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package CreatorAndConsumerBlock;

/**
* 生产者
*
* @author Martin
*/
public class Creator implements Runnable {
/**
* 线程资源
*/
private Plate plate;

public Creator(Plate plate) {
this.plate = plate;
}

@Override
public void run() {
Object egg = new Object();
plate.addEgg(egg);
}
}
48 changes: 48 additions & 0 deletions src/CreatorAndConsumerBlock/Plate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package CreatorAndConsumerBlock;

import java.util.concurrent.ArrayBlockingQueue;

/**
* 盘子,表示共享的资源
*
* @author Martin
*/
public class Plate {
/**
* 将arraylist换成阻塞队列,可见使用了阻塞队列后,代码中再也没有了lock、condition等了,完全不需要手动控制线程的等待还是唤醒了
*/
private ArrayBlockingQueue<Object> eggs = new ArrayBlockingQueue<Object>(99999);

/**
* 获取蛋
*
* @return
*/
public Object getEgg() {
try {
Object egg = eggs.take();
System.out.println("消费者取蛋,当前剩余:" + eggs.size());
return egg;
} catch (InterruptedException e) {
e.printStackTrace();
}

return null;
}


/**
* 加入蛋
*
* @return
*/
public void addEgg(Object egg) {
try {
eggs.put(new Object());
System.out.println("生产者生蛋,当前剩余:" + eggs.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
25 changes: 25 additions & 0 deletions src/CreatorAndConsumerBlock/Tester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package CreatorAndConsumerBlock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Tester {
public static void main(String[] args)
{
//共享资源
Plate plate = new Plate();
ExecutorService pool = Executors.newFixedThreadPool(100);

int nProducer = 20;
int nConsumer = 10;
for (int i = 0; i < nProducer; i++) {
pool.execute(new Creator(plate));

}
for (int i = 0; i < nConsumer; i++) {
pool.execute(new Consumer(plate));
}

pool.shutdown();
}
}
32 changes: 32 additions & 0 deletions src/Divide.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.ArrayList;
import java.util.List;

public class Divide {

public static void main(String[] args) {
System.out.println(method(18, 4).toString());

}

public static List<Integer> method(int N, int L) {
ArrayList<Integer> list = new ArrayList<>();
int length = 0;
int begin = 0;
for (int i = L; i <= 2 * N; i++) {
if ((2 * N) % i == 0) {
int tmp = (2 * N) / i - i + 1;
if (tmp > 0 && tmp % 2 == 0) {
length = i;
begin = tmp / 2;
break;
}
}
}
if (begin != 0) {
for (int i = begin; i < begin + length; i++)
list.add(i);
}
return list;
}

}
22 changes: 22 additions & 0 deletions src/ElegantPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import java.util.Scanner;
public class ElegantPoint {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sqr = in.nextInt();
in.close();
int ret = 0;
int tmp = 0;
for (int i = 0; i <= Math.sqrt(sqr / 2); i++) {
if (i * i + (tmp = (int) Math.sqrt(sqr - i * i)) * tmp == sqr) {
if (i == 0 || i == tmp) {
ret += 4;
} else {
ret += 8;
}
}
}
System.out.println(ret);
}
}
11 changes: 11 additions & 0 deletions src/FreshJuiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class FreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM , LARGE }
FreshJuiceSize size;
}

public class FreshJuiceTest {
public static void main(String []args){
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM;
}
}
Loading

0 comments on commit e9082a8

Please sign in to comment.