feat: 数据共享问题
This commit is contained in:
parent
20fb679804
commit
71ffb918f6
|
@ -0,0 +1,4 @@
|
||||||
|
package thead.feture;
|
||||||
|
|
||||||
|
public class FutureTask {
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package thead.thread_3;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MyThread myThread1 = new MyThread(1);
|
||||||
|
MyThread myThread2 = new MyThread(2);
|
||||||
|
MyThread myThread3 = new MyThread(3);
|
||||||
|
MyThread myThread4 = new MyThread(4);
|
||||||
|
MyThread myThread5 = new MyThread(5);
|
||||||
|
MyThread myThread6 = new MyThread(6);
|
||||||
|
MyThread myThread7 = new MyThread(7);
|
||||||
|
MyThread myThread8 = new MyThread(8);
|
||||||
|
MyThread myThread9 = new MyThread(9);
|
||||||
|
MyThread myThread10 = new MyThread(10);
|
||||||
|
|
||||||
|
myThread1.start();
|
||||||
|
myThread2.start();
|
||||||
|
myThread3.start();
|
||||||
|
myThread4.start();
|
||||||
|
myThread5.start();
|
||||||
|
myThread6.start();
|
||||||
|
myThread7.start();
|
||||||
|
myThread8.start();
|
||||||
|
myThread9.start();
|
||||||
|
myThread10.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package thead.thread_3;
|
||||||
|
|
||||||
|
public class MyThread extends Thread {
|
||||||
|
|
||||||
|
private final Integer integer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocates a new {@code Thread} object. This constructor has the same
|
||||||
|
* effect as {@linkplain #Thread(ThreadGroup, Runnable, String) Thread}
|
||||||
|
* {@code (null, null, gname)}, where {@code gname} is a newly generated
|
||||||
|
* name. Automatically generated names are of the form
|
||||||
|
* {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
|
||||||
|
*/
|
||||||
|
public MyThread(Integer integer) {
|
||||||
|
this.integer = integer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
super.run();
|
||||||
|
System.out.println(integer);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package thead.thread_4;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
// 有线程安全的操作
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MyThread t1 = new MyThread("t1");
|
||||||
|
MyThread t2 = new MyThread("t2");
|
||||||
|
MyThread t3 = new MyThread("t3");
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
t3.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package thead.thread_4;
|
||||||
|
|
||||||
|
public class Application2 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MyThread myThread = new MyThread("t0");
|
||||||
|
|
||||||
|
Thread t1 = new Thread(myThread, "t1");
|
||||||
|
Thread t2 = new Thread(myThread, "t2");
|
||||||
|
Thread t3 = new Thread(myThread, "t3");
|
||||||
|
Thread t4 = new Thread(myThread, "t4");
|
||||||
|
Thread t5 = new Thread(myThread, "t5");
|
||||||
|
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
t3.start();
|
||||||
|
t4.start();
|
||||||
|
t5.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package thead.thread_4;
|
||||||
|
|
||||||
|
public class MyThread extends Thread {
|
||||||
|
|
||||||
|
private Integer count = 10;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocates a new {@code Thread} object. This constructor has the same
|
||||||
|
* effect as {@linkplain #Thread(ThreadGroup, Runnable, String) Thread}
|
||||||
|
* {@code (null, null, gname)}, where {@code gname} is a newly generated
|
||||||
|
* name. Automatically generated names are of the form
|
||||||
|
* {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
|
||||||
|
*/
|
||||||
|
public MyThread(String name) {
|
||||||
|
super();
|
||||||
|
setName(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this thread was constructed using a separate
|
||||||
|
* {@code Runnable} run object, then that
|
||||||
|
* {@code Runnable} object's {@code run} method is called;
|
||||||
|
* otherwise, this method does nothing and returns.
|
||||||
|
* <p>
|
||||||
|
* Subclasses of {@code Thread} should override this method.
|
||||||
|
*
|
||||||
|
* @see #start()
|
||||||
|
* @see #stop()
|
||||||
|
* @see #Thread(ThreadGroup, Runnable, String)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public synchronized void run() {
|
||||||
|
super.run();
|
||||||
|
|
||||||
|
// while (count > 0) {
|
||||||
|
count--;
|
||||||
|
System.out.println("由" + currentThread().getName() + "计算,count=" + count);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue