package test4;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author abu
* @create 2019/7/29
* @since 1.0.0
*/
class SubThread1 extends Thread{
public void run(){
for (int i = 1; i <= 10; i ++){
if (i % 7 == 0){
//Thread.currentThread().yield();
/*try {
Thread.currentThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
/*try {
Thread.currentThread().sleep(100);
} catch (Exception e) {
e.printStackTrace();
}*/
System.out.println("线程名称: " + Thread.currentThread().getName() + "(●'◡'●)" +
"线程优先级: " +
Thread.currentThread().getPriority());
//System.out.println("下一个过来!");
}
}
}
public class TestThread {
public static void main(String[] args){
SubThread1 st = new SubThread1();
st.setName("这是子线程");
st.setPriority(10);
st.start();
Thread.currentThread().setName("这是主线程");
for (int i = 1; i <= 10; i ++){
/*if (i % 7 == 0){
//Thread.currentThread().yield();
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}*/
System.out.println("线程名称: " + Thread.currentThread().getName() + "(●'◡'●)" +
"线程优先级: " +
Thread.currentThread().getPriority());
//System.out.println("下一个过来!");
}
System.out.println(st.isAlive());
}
}
2、一个子线程输出1-100奇数,另一个子线程偶数,在主线程同时输出奇偶数
package test5;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author abu
* @create 2019/7/31
* @since 1.0.0
*/
class Thread1 extends Thread{
public void run(){
for(int i = 0; i < 100; i ++){
if (i%2 == 0){
System.out.println(Thread1.currentThread().getName() + ":" + i);
}
}
}
}
class Thread2 extends Thread{
public void run(){
for(int i = 0; i < 100; i ++){
if (i%2 != 0){
System.out.println(Thread1.currentThread().getName() + ":" + i);
}
}
}
}
public class TestThread {
public static void main(String[] args){
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.setName("偶数");
t2.setName("奇数");
t1.start();
t2.start();
new Thread(){
public void run(){
for(int i = 0; i < 100; i ++){
if (i%2 != 0){
System.out.println(Thread1.currentThread().getName() + ":" + i);
}
}
}
}.start();
new Thread(){
public void run(){
for(int i = 0; i < 100; i ++){
if (i%2 == 0){
System.out.println(Thread1.currentThread().getName() + ":" + i);
}
}
}
}.start();
}
}
3、TestMain
package test5;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author abu
* @create 2019/7/31
* @since 1.0.0
*/
public class TestMain {
public static void main(String[] args){
method2("what's your problem!");
}
public static void method1(String str){
System.out.println("method1");
System.out.println(str);
}
public static void method2(String str){
System.out.println("method2");
method1(str);
}
}
4、
public class TestWindow2 {
public static void main(String[] args){
Window2 w1 = new Window2();
Window2 w2 = new Window2();
Window2 w3 = new Window2();
w1.setName("一");
w2.setName("二");
w3.setName("三");
w1.start();
w2.start();
w3.start();
}
}
class Window2 extends Thread{
static int ticket = 50;
Object obj = new Object();
public void run(){
while (true){
synchronized(obj){
if (ticket > 0){
System.out.println(Thread.currentThread().getName() + "窗口售票:" + ticket--);
}else {
break;
}
}
}
}
}
5、
public class TestWindow1 {
public static void main(String[] args){
Window1 w = new Window1();
Thread w1 = new Thread(w);
Thread w2 = new Thread(w);
Thread w3 = new Thread(w);
w1.setName("窗口一");
w2.setName("窗口二");
w3.setName("窗口三");
w1.start();
w2.start();
w3.start();
}
}
class Window1 implements Runnable{
static int ticket = 50;
public void run(){
while (true){
synchronized(this){
if (ticket > 0){
System.out.println(Thread.currentThread().getName() + "票号:" + ticket --);
}else{
break;
}
}
}
}
}
6、
public class TestWindow3 {
public static void main(String[] args){
Window3 w = new Window3();
Thread w1 = new Thread(w);
Thread w2 = new Thread(w);
Thread w3 = new Thread(w);
w1.setName("一");
w2.setName("二");
w3.setName("三");
w1.start();
w2.start();
w3.start();
}
}
class Window3 implements Runnable{
int ticket = 100;
public void run(){
while (true){
show();
}
}
public synchronized void show(){
if (ticket > 0){
System.out.println(Thread.currentThread().getName() + "窗口卖" + ticket--);
}
}
}
7、
public class TestSingleton {
public static void main(String[] args){
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1==s2);
Class clazz = Singleton.class;
System.out.println(clazz);
}
}
class Singleton{
private Singleton(){
}
private static Singleton instance = null;
public static Singleton getInstance(){
if (instance == null){
synchronized(Singleton.class){
if (instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
8、
public class TestProduct {
public static void main(String[] args){
Clerk clerk = new Clerk();
Producer p = new Producer(clerk);
Consumer c = new Consumer(clerk);
Thread t1 = new Thread(p);
Thread t3 = new Thread(p);
Thread t2 = new Thread(c);
t1.setName("生产者一");
t2.setName("消费者");
t3.setName("生产者二");
t1.start();
t2.start();
t3.start();
}
}
class Clerk{
int product;
public synchronized void addProduct(){
while (true){
if (product >= 20){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
product ++;
System.out.println(Thread.currentThread().getName() + "生产了" + product + "个产品");
notifyAll();
}
}
}
public synchronized void consumeProduct(){
while (true){
if (product <= 0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
System.out.println(Thread.currentThread().getName() + "消费了" + product + "个产品");
product --;
notifyAll();
}
}
}
}
class Producer implements Runnable{
Clerk clerk;
public Producer(Clerk clerk){
this.clerk = clerk;
}
public void run(){
System.out.println("生产者生产产品");
while (true){
try {
Thread.currentThread().sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
clerk.addProduct();
}
}
}
class Consumer implements Runnable{
Clerk clerk;
public Consumer(Clerk clerk){
this.clerk = clerk;
}
public void run(){
System.out.println("消费者消费产品");
while (true){
try {
Thread.currentThread().sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
clerk.consumeProduct();
}
}
}