Java
Servlet+MySQL后台与前台Android的配合使用
环境:Tomcat7.059 MySQL5.6.25
1.创建数据库:
CREATE DATABASE [databasename];
2.创建表:
CREATE TABLE [tablename] (id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,code INT UNSIGNED NOT NULL,name VARCHA(20),title VARCHAR(20) NOT NULL);
3.添加一条测试记录:
INSERT [tablename] VALUE(null,111,”张三”,”学生”);
4.建立数据类(Dbinfo.java):
初学Java:多线程(六)
经典的生产者消费者程序,多线程,解决互斥问题,代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
package com.test; public class ProducerConsumer { public static void main(String[] args) { SynStack ss = new SynStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); Thread t = new Thread(p); t.start(); Thread t2 = new Thread(c); t2.start(); } } //定义一个窝头类 class WoTou { int id; WoTou( int id) { this.id = id; } public String toString() { return "WoTou :" +id; } } //定义篓子类用 来放窝头 class SynStack { WoTou[] ss = new WoTou[8]; int index =0; public synchronized void push(WoTou wt) { while(index==ss.length) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); ss[index]=wt; index+ +; } public synchronized WoTou pull() { while(index==0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); index--; return ss[index]; } } //定义生 产者类 class Producer implements Runnable { SynStack ss = null; public Producer(SynStack ss) { this.ss = ss; } public void run() { for(int i=0;i<20;i++) { WoTou wt = new WoTou(i); ss.push(wt); System.out.println( "生产了"+wt); try { Thread.sleep((long) (Math.random()*100)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { SynStack ss = null; public Consumer(SynStack ss) { this.ss = ss; } public void run() { for(i nt i=0;i<20;i++) { WoTou wt = ss.pull(); System.out.println( "消费了"+wt); try { Thread.sleep( (long) (Math.random()*1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
初学Java:多线程(五)
根据“哲学家就餐”问题,写一个典型的多线程死锁程序:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public class DeadLock implements Runnable { static Object o1 = new Object(); static Object o2 = new Object(); public int flag = 1; public static void main(String[] args) { DeadLock d1 = new DeadLock(); DeadLock d2 = new DeadLock(); d1.flag = 1; d2.flag = 0; Thread t1 = new Thread(d1); Thread t2 = new Thread(d2); t1.start(); t2.start(); } public void run() { System.out.println("flag:"+flag); if(flag==0) { synchronized(o1) { try { Thread.sleep(1000); }catch(Exception e) { e.printStackTrace(); } synchronized(o2) { System.out.println("1"); } } } if(flag==1) { synchronized(o2) { try { Thread.sleep(1000); }catch(Exception e) { e.printStackTrace(); } synchronized(o1) { System.out.println("0"); } } } } } |
初学Java:多线程(三)
初学Java:多线程(二)
一、多线程相当于一个程序的多条运行路径,当一个JAVA的程序启动,后台默认开启的是两个线程,一个Main线程,一个垃圾回收线程:
上图是一个典型的JAVA程序,当Main线程启动后遇到方法1,则进入方法1进行执行,而方法1中又有方法2,则进入方法2,直到方法2执行完毕后返回方法1继续执行,而后在方法1执行完毕后再返回Main线程,直到Main线程结束。
初学Java:多线程(一)
同包中的两个类,A类与B类,当B类需要调用A类中的数组或方法时,应首先在B类中对A类实例化,而非在函数中实例化,否则会出现找不到A类数组的错误提示。要特别留意!
启用线程,有.run()与.start()两种,当一个类使用继承线程(extends)时,启动线程是.start(),若一个类使用的进程接口(implements)时,启动线程则是.run(),那么它们有什么区别呢?群里的朋友说,.start()更安全。真的是这样吗?(看书)
若类使用的进程接口时,还需要将runnable传给thread,然后使用.start()开启。否则无法实现多线程的轮番执行,只会等一个线程完全执行完才执行下一个线程。切记,切记!