java应用技术 1(2)

java应用技术
1(2)
1.生产者和消费者
案例:
package cn.java.java1;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class LockPSTheadsDemo1 {
public static void main(String[] args){
prodution4 pt = new prodution4();
produter4 pr  =new produter4(pt);
saler4 s = new saler4(pt);
Thread th1 = new Thread(pr);
Thread th2 = new Thread(s);
Thread th3 = new Thread(pr);
Thread th4 = new Thread(s);
th1.setName("叶良辰:");
th2.setName("叶良辰:");
th3.setName("叶良辰:");
th4.setName("叶良辰:");
th1.start();
th2.start();
th3.start();
th4.start();
}
}


class prodution4{
private String name;
private int i;
private boolean flag = false; 
private Lock lock = new ReentrantLock();
private Condition c_p = lock.newCondition();
private Condition c_s = lock.newCondition();
public prodution4() {
}
public prodution4(String name, int i,boolean flag) {
this.name = name;
this.i = i;
this.flag = false;
}

public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public void produtting(){
lock.lock();
while(this.isFlag()){
try {
c_p.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setI(i+1);
this.setName("赵日天");
System.out.println(Thread.currentThread().getName()+"生产"+this.getName()+this.getI()+"号\n");
this.setFlag(true);
c_s.signal();
lock.unlock();
}
public void salseing(){
lock.lock();
while(!this.isFlag()){
try {
c_s.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"销售"+this.getName()+this.getI()+"号\n");
this.setFlag(false);
c_p.signal();
lock.unlock();
}
}
class produter4 implements Runnable{
private prodution4 p = null;
public produter4(){
}
public produter4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub


for(int a = 0;a<100;a++){
p.produtting();


}


}

}
class saler4 implements Runnable{
private prodution4 p = null;
public saler4(){
}
public saler4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int a = 0;a<100;a++){
p.salseing();

}
}

}
2.用同步代码块实现生产者和消费者
案例:
package cn.java.java1;
public class PSTheadsDemo {
public static void main(String[] args) {
Production1 p = new Production1();
producter1 pr1 = new producter1(p);
saler1 s1 = new saler1(p);
producter1 pr2 = new producter1(p);
saler1 s2 = new saler1(p);
Thread th1 = new Thread(pr1);
Thread th2 = new Thread(pr2);
Thread th3 = new Thread(s1);
Thread th4 = new Thread(s2);
th1.setName("生产窗口1:"+"\n");
th2.setName("生产窗口2:"+"\n");
th3.setName("出售窗口1:"+"\n");
th4.setName("出售窗口2:"+"\n");
th1.start();
th2.start();
th3.start();
th4.start();
}
}


class Production1 {
private String name;
private int i;
private boolean flag = false;


public Production1() {
}


public Production1(boolean flag) {
this.flag = flag;
}


public Production1(String name, int i) {
this.name = name;
this.i = i;
}


public boolean isFlag() {
return flag;
}


public void setFlag(boolean flag) {
this.flag = flag;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getI() {
return i;
}


public void setI(int i) {
this.i = i;
}


public synchronized void productting() {
for (int c = 0; c < 100; c++) {
while (this.isFlag()) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i++;
this.setName("赵日天");
System.out.println(Thread.currentThread().getName() + "生产"
+ this.getName() + this.getI() + "号"+"\n");
this.notifyAll();
this.setFlag(true);
}
}


public synchronized void salerring() {
for (int c = 0; c < 100; c++) {
while (!this.isFlag()) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "出售"
+ this.getName() + this.getI() + "号"+"\n");
this.notifyAll();
this.setFlag(false);
}
}
}


class producter1 implements Runnable {
private Production1 p = null;


public producter1() {
}


public producter1(Production1 p) {
this.p = p;
}


public Production1 getP() {
return p;
}


public void setP(Production1 p) {
this.p = p;
}


@Override
public void run() {
// TODO Auto-generated method stub
p.productting();
}


}


class saler1 implements Runnable {
private Production1 p = null;


public saler1() {
}


public saler1(Production1 p) {
this.p = p;
}


public Production1 getP() {
return p;
}


public void setP(Production1 p) {
this.p = p;
}


@Override
public void run() {
// TODO Auto-generated method stub
p.salerring();
}


}
3.监视器实现生产者和消费者
案例:
package cn.java.java1;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class LockPSTheadsDemo1 {
public static void main(String[] args){
prodution4 pt = new prodution4();
produter4 pr  =new produter4(pt);
saler4 s = new saler4(pt);
Thread th1 = new Thread(pr);
Thread th2 = new Thread(s);
Thread th3 = new Thread(pr);
Thread th4 = new Thread(s);
th1.setName("叶良辰:");
th2.setName("叶良辰:");
th3.setName("叶良辰:");
th4.setName("叶良辰:");
th1.start();
th2.start();
th3.start();
th4.start();
}
}


class prodution4{
private String name;
private int i;
private boolean flag = false; 
private Lock lock = new ReentrantLock();
private Condition c_p = lock.newCondition();
private Condition c_s = lock.newCondition();
public prodution4() {
}
public prodution4(String name, int i,boolean flag) {
this.name = name;
this.i = i;
this.flag = false;
}

public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public void produtting(){
lock.lock();
while(this.isFlag()){
try {
c_p.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setI(i+1);
this.setName("赵日天");
System.out.println(Thread.currentThread().getName()+"生产"+this.getName()+this.getI()+"号\n");
this.setFlag(true);
c_s.signal();
lock.unlock();
}
public void salseing(){
lock.lock();
while(!this.isFlag()){
try {
c_s.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"销售"+this.getName()+this.getI()+"号\n");
this.setFlag(false);
c_p.signal();
lock.unlock();
}
}
class produter4 implements Runnable{
private prodution4 p = null;
public produter4(){
}
public produter4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub


for(int a = 0;a<100;a++){
p.produtting();


}


}

}
class saler4 implements Runnable{
private prodution4 p = null;
public saler4(){
}
public saler4(prodution4 p) {
this.p = p;
}

public prodution4 getP() {
return p;
}
public void setP(prodution4 p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int a = 0;a<100;a++){
p.salseing();

}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java桌面应用开发技术是指使用Java语言和相关的框架和工具来开发能够在桌面环境下运行的应用程序。Java提供了丰富的库和工具,使得开发者可以轻松地创建功能强大、跨平台的桌面应用程序。 Java桌面应用开发技术主要包括以下几个方面: 1. Java Swing:Java Swing是Java提供的一个GUI(图形用户界面)工具包,它提供了一系列的组件和容器,可以用于创建各种界面元素,如按钮、文本框、标签等。Swing还提供了布局管理器,用于控制组件的位置和大小。使用Swing可以创建出美观、交互性强的桌面应用程序。 2. JavaFX:JavaFX是Java官方推出的一套富客户端应用程序开发工具包。它提供了丰富的UI组件和布局容器,支持CSS样式和动画效果,并且集成了多媒体和图形渲染功能。JavaFX具有良好的可扩展性和跨平台性,可以用于开发各种类型的桌面应用程序。 3. AWT(Abstract Window Toolkit):AWT是Java最早提供的GUI工具包,它提供了一组基本的GUI组件和布局管理器。AWT的特点是简单易用,但功能相对较少,界面风格也比较古老。目前,AWT主要被Swing和JavaFX所取代,但在一些特殊场景下仍然有一定的应用。 4. 第三方框架:除了Java官方提供的GUI工具包外,还有一些第三方框架可以用于Java桌面应用开发,如SWT(Standard Widget Toolkit)、Java Native Interface(JNI)等。这些框架提供了更多的功能和灵活性,可以满足一些特殊需求。 总结起来,Java桌面应用开发技术主要包括Java Swing、JavaFX、AWT和第三方框架等。开发者可以根据项目需求和个人喜好选择合适的技术进行开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值