Java 线程编程中的同步、重复、定时
(一)线程同步
实现生产者消费者问题来说明线程问题,举例如下所示:
/**
* 生产者消费者问题
*/
public class ProducerConsumer {
/**
* 主方法
*/
public static void main(String[] args) {
ProductBox pb = new ProductBox();
Producer p = new Producer(pb);
Consumer c = new Consumer(pb);
//根据消费者、生产者生成线程
Thread pThread = new Thread(p);
Thread cThread = new Thread(c);
pThread.setPriority(Thread.MAX_PRIORITY);
pThread.start();
cThread.start();
}
}
/**
* 产品对象
* @author johsnton678
*/
class Product {
int id;
public Product(int id) {
super();
this.id = id;
}
public String toString(){
return "Product:" + id;
}
}
/**
* 产品盒对象
* @author johnston678
*/
class ProductBox {
Product[] productbox = new Product[6];
int index = 0;
public ProductBox() {
super();
}
//存入方法,将产品存入产品盒
public synchronized void push(Product p) {
//判断产品是否超出产品盒的最大容量,如果超出中则等待
while (index == productbox.length) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.notify();
productbox[index] = p;
index ++;
}
//取出操作
public synchronized Product pop() {
//如果产
相关文档:
插入式排序运行效率N*(N-1)/4 对于随机数字,这个算法比冒泡快1倍,比选择排序稍微快一点.
如果是基本有序的队列则优势最为明显需要O(N)
代码一样是从冒泡排序继承下来的.
/**
*
* @author leon.lee
*/
public class InsertSort extends BubbleSort {
public InsertSort(int lengthArray){
......
当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个java对象转换为字节序列,即java对象序列号,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象,即反序列化。
把Java对象转换为字节序列的过程称为对象的序列化。
......
再次从网上查询,搜到了RXTXcomm.jar包比较好,是封装了comm.jar的方法。
安装:
1.copy rxtxSerial.dll to [JDK-directory]\jre\bin\rxtxSerial.dll
2.copy RXTXcomm.jar to [JDK-directory]\jre\lib\ext\RXTXcomm.jar
&nbs ......
package com.njty.util;
public class Test {
private static final double EARTH_RADIUS = 6378137;
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
  ......
为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程。汉语编程是怎么回事我也不大清楚,应该是前年吧,我一朋友给我介绍汉语编程,怎么不错不错?当时因为学习忙没去关注这个,等我闲 ......