You are on page 1of 38

Lp trnh ng dng MiniChat bng Java

dng c ch x l Multithread

TS. Nguyn Vn Hip,


Nguyn Quang Hng
E-mail: hungnq2@dit.hcmut.edu.vn
Website: www.dit.hcmut.edu.vn/~hungnq/courses.htm
Mc tiu
1. S dng c ch Java multithread.
2. Bi tp ng dng MiniChat cho php lm
quen vi qui trnh xy dng 1 chng
trnh theo giao din Dialog based bng
JBuilder.

Faculty of Information Technology, HCM University


of Technology
Mc lc
1. Gii thiu c.trnh MiniChat
2. K thut lp trnh Java Multi-thread
3. Xy dng ng dng Minichat bng JBuilder
a) Thit k giao din cho MiniChat Client/Server
b) Thit k MiniChat server p ng cc kt ni
ng thi t cc client bng c ch multithread

Faculty of Information Technology, HCM University


of Technology
Gii thiu v ctrnh. MiniChat
Chc nng: cho php nhiu user ng k vo cc nhm tr chuyn
vi nhau.
M hnh la chn: client/server
Server: qun l cc nhm v cc user ca tng nhm, phn phi chui
thng tin t mt user n cc user khc.
Client: giao tip vi cc user; cho php h ng k nhm; gi/nhn
thng tin cho nhau.
nh ngha giao thc dng cho h thng MiniChat:
Lnh GLIST <CRLF>
Lnh ULIST <CRLF>
Lnh LOGIN <tn group>,<tn user> <CRLF>
Lnh SEND <string> <CRLF>
Lnh LOGOU <CRLF>
Dng reply cho tt c cc request:
N <chui d liu ph km theo>
N = 1: Thnh cng, N = 0: Tht bi

Faculty of Information Technology, HCM University


of Technology
Giao din ca ch.trnh MiniChat client
Ca s nhp thng tin ca
button Connect

Ca s nhp thng tin ca


button login

Faculty of Information Technology, HCM University


of Technology
Giao din ca MS. Visual C++ IDE
Chy Microsoft Visual C++t Start Programs Microsoft Visual C++

Faculty of Information Technology, HCM University


of Technology
K thut lp trnh Java Multithread

Ti liu tham kho:


java.sun.com
K thut Java Multithread
Thread l mt lung thc thi trong mt
chng trnh. My o JVM cho php mt
ng dng c nhiu lung thc thi ng thi.

C 2 cch dng Java multithread (a lung):


Khai bo mt lp k tha t lp Thread v
override method Thread.run().
Khai bo mt lp hin thc interface Runnable
v method Runnable.run()
Faculty of Information Technology, HCM University
of Technology
Cch 1: K tha t lp Thread
1. class PrimeThread extends Thread {
2. long minPrime;
3. PrimeThread( long minPrime ) {
4. this.minPrime = minPrime;
5. }
6. public void run( ) {
7. // compute primes larger than minPrime
8. ...
9. }
10. }

11. PrimeThread p = new PrimeThread(143);


12. p.start();
Faculty of Information Technology, HCM University
of Technology
Cch 2: Hin thc giao din Runnable
1. class PrimeRun implements Runnable {
2. long minPrime;
3. PrimeRun ( long minPrime ) {
4. this.minPrime = minPrime;
5. }

6. public void run() {


7. // compute primes larger than minPrime
8. ...
9. }
10. }

11. PrimeRun p = new PrimeRun(143);


12. new Thread(p).start();

Faculty of Information Technology, HCM University


of Technology
Thread Cc constructor ca lp Thread
Thread( ) - Allocates a new Thread object.
Thread ( Runnable target)
Thread (Runnable target, String name)
Thread (String name)
Thread (ThreadGroup group, Runnable target)
Thread (ThreadGroup group, Runnable target, String name)
To ra 1 thread mi, target l i tng c method run chy, tn
c xc nh bi name, thuc v 1 group.
Thread (ThreadGroup group, Runnable target, String name,
long stackSize)
Ging trn nhng c thm kch thc stack ca thread mi.
Thread (ThreadGroup group, String name)

Faculty of Information Technology, HCM University


of Technology
Thread Cc method khc
void start()
void run()
boolean isAlive()
get/setName()
static void sleep()
setDaemon (boolean on); isDaemon()
Cc method join()

Faculty of Information Technology, HCM University
of Technology
Xy dng ng dng MiniChat bng
JBuilder
Xy dng ng dng MiniChat bng JBuilder

Faculty of Information Technology, HCM University


of Technology
ng cc projects ang m
Chn menu File.Close projects
Chn button All ng tt c projects ang m

Faculty of Information Technology, HCM University


of Technology
To project mi tn MiniChatClient
Chn File.New projects

Faculty of Information Technology, HCM University


of Technology
Project Wizard Step 1 of 3
Nhp tn Project, la chn th mc lu project, chn Template
(nn default project), ri chn Next

Faculty of Information Technology, HCM University


of Technology
Project Wizard Step 2 of 3
Chn JDK (1.4.1), chn cc ng dn lu cc lp, c.trnh
ngun, th mc Working (nn mc nh)

Faculty of Information Technology, HCM University


of Technology
Project Wizard Step 2 of 3
Thm cc th vin dng km theo (nu c) tab Required
Libraries. Ri chn NEXT

Faculty of Information Technology, HCM University


of Technology
Project Wizard Step 3 of 3
Xc nh m ha (UTF-8), thm cc thng tin khc v tn d n,
m t v chn FINISH

Faculty of Information Technology, HCM University


of Technology
To 1 ng dng trong project MiniChatClient
Chn menu File.New m Object Gallery. Chn icon Application
to mt ng dng mi trong project MiniChatClient

Faculty of Information Technology, HCM University


of Technology
Application Wizard Step 1 of 3
Nhp tn ng dng, chn package cho cc class s to ra, tn lp
Application mi ny.

Faculty of Information Technology, HCM University


of Technology
Application Wizard Step 2 of 3
Nhp tn class, ta , chn cc mc to status bar, About
dialog Ri chn NEXT

Faculty of Information Technology, HCM University


of Technology
Application Wizard Step 3 of 3
Nhp tn qun l cu hnh run time (nn mc nh). Ri n nt
FINISH to ng dng MiniChatClient

Faculty of Information Technology, HCM University


of Technology
To ng dng c.trnh MiniChatClient

Faculty of Information Technology, HCM University


of Technology
Thit k giao din
Chn tab Design chuyn sang mode thit k giao din bng cc
control JComponents nh JButton, JList, JPanel

Faculty of Information Technology, HCM University


of Technology
Thit k giao din Dng XYLayout
Chn mc Content Pane, thay i thuc tnh Layout thnh XYLayout
c th thit k GUI dng t do da trn ta trc X v Y.

Faculty of Information Technology, HCM University


of Technology
Thit k giao din ko cc thnh phn
Chn mc tab SWING, chn icon JButton ri v n gc tri trn
form, chn button va v quan st v gn gi tr cho thuc tnh
name=btnConnect, margin=1,1,1,1, thuc tnh text = Connect

Faculty of Information Technology, HCM University


of Technology
Thit k giao din to cc nt

-Lp li cc bc
trc ko
thm cc nt:
Groups, Login,
Users, Send,
Logout

Faculty of Information Technology, HCM University


of Technology
Thit k giao din to danh sch nhm

- Chn tab
Swing
Containers,
chn icon
JScrollPane ri
v crollpane
cha listbox hin
th danh sch
nhm, thit lp
field name =
jspGroups

Faculty of Information Technology, HCM University


of Technology
Thit k giao din

-Lp li bc va
ri to cc danh
sch jlbUsers,
jlbContent.
- V mt
JTextField, v thit
lp field name =
jtfMessage.
- C th canh tri,
trn, phi cc
thnh phn.

Faculty of Information Technology, HCM University


of Technology
Thit k giao din Hon tt

Faculty of Information Technology, HCM University


of Technology
To hm nm bt cc s kin

- Chn nt
Connect, chn
tab Events, a
cursor v thuc
tnh
ActionPerforme
d, ri n Enter
chn hm
x l vi tn
mc nh

Faculty of Information Technology, HCM University


of Technology
To hm nm bt cc s kin (tip)

- Vit code cho hm


nm bt s kin n
nt Connect.

- Lp li qu trnh
ny cho cc nt
khc nh Groups,
Login, Users, Send,
Logout.

Faculty of Information Technology, HCM University


of Technology
M hnh multi-thread client

Faculty of Information Technology, HCM University


of Technology
M hnh lp trnh multi-thread server

Faculty of Information Technology, HCM University


of Technology
Code pha Server
// code ca file MiniChatServerDlg
package jbminichatclient;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.sql.*;
Faculty of Information Technology, HCM University
of Technology
Code pha Server (tip)
public class MiniChatServerDlg extends JFrame implements
MessageListener {
//cc thuc tnh cn dng cho Server
int m_groupcnt; // s nhm
T_UserRec m_sock_no_user=null; // danh sch cc user cha
login
T_GroupList m_grouplist[] = new T_GroupList[10]; // danh
sch nhm
String uname; // tn user
ServerSocket serverSocket; // socket m server dng accept
final int SERVER_PORT = 256; // port lng nghe ca server
//cc thuc tnh cn dng cho Server
JPanel contentPane;
Faculty of Information Technology, HCM University
of Technology

You might also like